diff --git a/src/main/java/com/jozufozu/flywheel/api/Material.java b/src/main/java/com/jozufozu/flywheel/api/Material.java index f6a963cf7..69956a8e0 100644 --- a/src/main/java/com/jozufozu/flywheel/api/Material.java +++ b/src/main/java/com/jozufozu/flywheel/api/Material.java @@ -19,6 +19,7 @@ public interface Material { * * @param key An object that uniquely identifies the model. * @param modelSupplier A factory that creates the Model that you want to render. + * Each time {@link Supplier#get()} is called, a new Model must be created. * @return An instancer for the given model, capable of rendering many copies for little cost. */ Instancer model(Object key, Supplier modelSupplier); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterial.java index cf97a5b1b..4e256f08c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterial.java @@ -42,4 +42,9 @@ public class BatchedMaterial implements Material { models.values() .forEach(CPUInstancer::clear); } + + public void delete() { + models.values().forEach(CPUInstancer::delete); + models.clear(); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java index 37787e7c6..16aa5402e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java @@ -67,6 +67,9 @@ public class BatchedMaterialGroup implements MaterialGroup { } public void delete() { + materials.values() + .forEach(BatchedMaterial::delete); + materials.clear(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java index aa6e7f683..04220410a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java @@ -64,11 +64,13 @@ public class BatchingEngine implements Engine { @Override public void delete() { + for (Map groups : layers.values()) { + groups.values().forEach(BatchedMaterialGroup::delete); + } } @Override public void beginFrame(Camera info) { - } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java index 828827103..f4f052875 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java @@ -73,4 +73,8 @@ public class CPUInstancer extends AbstractInstancer { public void notifyDirty() { // noop } + + public void delete() { + modelData.delete(); + } } 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 d3522ac81..448e319d4 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 @@ -104,6 +104,8 @@ public class GPUInstancer extends AbstractInstancer { instanceVBO.delete(); vao.delete(); + + modelData.delete(); } protected void renderSetup() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java index 7a8373136..344917388 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java @@ -43,6 +43,10 @@ public class InstancedMaterial implements Material { }); } + public Collection> getAllInstancers() { + return models.values(); + } + public int getInstanceCount() { return models.values().stream().mapToInt(GPUInstancer::getInstanceCount).sum(); } @@ -57,11 +61,6 @@ public class InstancedMaterial implements Material { .allMatch(GPUInstancer::isEmpty); } - public void delete() { - models.values().forEach(GPUInstancer::delete); - models.clear(); - } - /** * Clear all instance data without freeing resources. */ @@ -70,7 +69,8 @@ public class InstancedMaterial implements Material { .forEach(GPUInstancer::clear); } - public Collection> getAllInstancers() { - return models.values(); + public void delete() { + models.values().forEach(GPUInstancer::delete); + models.clear(); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java index 2708f474d..4edc1b951 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java @@ -116,7 +116,6 @@ public class InstancingEngine

implements Engine { @Override public void delete() { for (Map> groups : layers.values()) { - groups.values().forEach(InstancedMaterialGroup::delete); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java index f190aa103..241638e43 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java @@ -14,6 +14,10 @@ public class ArrayModelRenderer { this.model = model; } + public Model getModel() { + return model; + } + /** * Renders this model, checking first if there is anything to render. */ 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 25546213e..b58f4ec7b 100644 --- a/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java +++ b/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java @@ -1,7 +1,10 @@ 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.VertexList; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.core.Formats; @@ -26,12 +29,14 @@ public class ModelPart implements Model { this.vertices = vertices; } - PosTexNormalWriterUnsafe writer = Formats.POS_TEX_NORMAL.createWriter(MemoryTracker.create(size())); + ByteBuffer buffer = MemoryTracker.create(size()); + PosTexNormalWriterUnsafe writer = Formats.POS_TEX_NORMAL.createWriter(buffer); for (PartBuilder.CuboidBuilder cuboid : cuboids) { cuboid.buffer(writer); } reader = writer.intoReader(); + MemoryUtil.memFree(buffer); } public static PartBuilder builder(String name, int sizeU, int sizeV) { diff --git a/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java index fa209c2d6..f4b8f1912 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java @@ -81,6 +81,6 @@ public final class WorldModelBuilder implements Bufferable { } public BlockModel intoMesh(String name) { - return new BlockModel(ModelUtil.getBufferBuilder(this), name); + return new BlockModel(this, name); } }