From f47f547930f682d4d841112f1e5713dc0a40a266 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Sun, 6 Nov 2022 14:00:24 -0800 Subject: [PATCH] Models are temporary - Add Model.delete - Allow BlockModel.createEBO to be called more than once for a single instance - Clear cache on QuadConverter.delete --- .../instancing/instancing/GPUInstancer.java | 3 +- .../jozufozu/flywheel/core/QuadConverter.java | 1 + .../flywheel/core/hardcoded/ModelPart.java | 11 +++ .../flywheel/core/model/BlockModel.java | 81 +++++++++++++++---- .../jozufozu/flywheel/core/model/Model.java | 2 + 5 files changed, 79 insertions(+), 19 deletions(-) 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 a127b8e30..d3522ac81 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 @@ -69,7 +69,6 @@ public class GPUInstancer extends AbstractInstancer { vao = new GlVertexArray(); - // XXX Callback seems unnecessary. Remove and extract code to run after alloc call? model = modelAllocator.alloc(modelData, arenaModel -> { // XXX VAO is bound and not reset or restored vao.bind(); @@ -77,7 +76,7 @@ public class GPUInstancer extends AbstractInstancer { arenaModel.setupState(vao); }); - // XXX VAO is already guaranteed to be bound in model callback + // XXX VAO is bound and not reset or restored vao.bind(); vao.enableArrays(model.getAttributeCount() + instanceFormat.getAttributeCount()); diff --git a/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java b/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java index 37ba431b7..266f15489 100644 --- a/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java +++ b/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java @@ -79,6 +79,7 @@ public class QuadConverter { public void delete() { GL32.glDeleteBuffers(ebo); + this.cache.clear(); this.quadCapacity = 0; } 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 d67ca22d3..a0c8978bc 100644 --- a/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java +++ b/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java @@ -59,4 +59,15 @@ public class ModelPart implements Model { return QuadConverter.getInstance() .quads2Tris(vertices / 4); } + + @Override + public void delete() { + if (reader instanceof AutoCloseable closeable) { + try { + closeable.close(); + } catch (Exception e) { + // + } + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java index 583eaeaab..176763966 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java @@ -19,6 +19,7 @@ import com.mojang.blaze3d.platform.MemoryTracker; import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexFormat.IndexType; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.model.BakedModel; @@ -32,7 +33,7 @@ public class BlockModel implements Model { private final String name; - private final Supplier eboSupplier; + private final EBOSupplier eboSupplier; public BlockModel(BlockState state) { this(Minecraft.getInstance() @@ -61,22 +62,7 @@ public class BlockModel implements Model { reader = Formats.BLOCK.createReader(renderedBuffer, pair.second()); if (!drawState.sequentialIndex()) { - ByteBuffer src = renderedBuffer.indexBuffer(); - ByteBuffer indexBuffer = MemoryTracker.create(src.capacity()); - MemoryUtil.memCopy(src, indexBuffer); - eboSupplier = () -> { - int vbo = GL32.glGenBuffers(); - - // XXX ARRAY_BUFFER is bound and restored - var bufferType = GlBufferType.ARRAY_BUFFER; - var oldBuffer = bufferType.getBoundBuffer(); - bufferType.bind(vbo); - GL15.glBufferData(bufferType.glEnum, indexBuffer, GlBufferUsage.STATIC_DRAW.glEnum); - bufferType.bind(oldBuffer); - MemoryUtil.memFree(indexBuffer); - - return new ElementBuffer(vbo, drawState.indexCount(), drawState.indexType()); - }; + eboSupplier = new BufferEBOSupplier(renderedBuffer.indexBuffer(), drawState.indexCount(), drawState.indexType()); } else { eboSupplier = () -> QuadConverter.getInstance() .quads2Tris(vertexCount() / 4); @@ -107,4 +93,65 @@ public class BlockModel implements Model { public VertexType getType() { return Formats.BLOCK; } + + @Override + public void delete() { + if (reader instanceof AutoCloseable closeable) { + try { + closeable.close(); + } catch (Exception e) { + // + } + } + eboSupplier.delete(); + } + + private interface EBOSupplier extends Supplier { + default void delete() { + } + } + + private static class BufferEBOSupplier implements EBOSupplier { + private final ByteBuffer indexBuffer; + private final int indexCount; + private final IndexType indexType; + + private int eboName = -1; + private ElementBuffer ebo; + + public BufferEBOSupplier(ByteBuffer indexBufferSrc, int indexCount, IndexType indexType) { + indexBuffer = MemoryTracker.create(indexBufferSrc.capacity()); + MemoryUtil.memCopy(indexBufferSrc, indexBuffer); + this.indexCount = indexCount; + this.indexType = indexType; + } + + @Override + public ElementBuffer get() { + if (eboName == -1) { + eboName = createEBO(); + ebo = new ElementBuffer(eboName, indexCount, indexType); + MemoryUtil.memFree(indexBuffer); + } + + return ebo; + } + + private int createEBO() { + int vbo = GL32.glGenBuffers(); + + // XXX ARRAY_BUFFER is bound and restored + var bufferType = GlBufferType.ARRAY_BUFFER; + var oldBuffer = bufferType.getBoundBuffer(); + bufferType.bind(vbo); + GL15.glBufferData(bufferType.glEnum, indexBuffer, GlBufferUsage.STATIC_DRAW.glEnum); + bufferType.bind(oldBuffer); + return vbo; + } + + @Override + public void delete() { + GL32.glDeleteBuffers(eboName); + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/Model.java b/src/main/java/com/jozufozu/flywheel/core/model/Model.java index ebe6dee47..b2e47c01d 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/Model.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/Model.java @@ -58,6 +58,8 @@ public interface Model { */ ElementBuffer createEBO(); + void delete(); + /** * The size in bytes that this model's data takes up. */