Models are (actually) temporary

- Call Model#delete on instancer delete
- Add constraint to modelSupplier in Material#model
- Fix memory leak in ModelPart
- Add ArrayModelRenderer#getModel
This commit is contained in:
PepperCode1 2022-11-22 13:04:03 -08:00
parent f6d5ed01e7
commit f56d0f9024
11 changed files with 36 additions and 11 deletions

View file

@ -19,6 +19,7 @@ public interface Material<D extends InstanceData> {
*
* @param key An object that uniquely identifies the model.
* @param modelSupplier A factory that creates the Model that you want to render.
* <b>Each time {@link Supplier#get()} is called, a new Model must be created.</b>
* @return An instancer for the given model, capable of rendering many copies for little cost.
*/
Instancer<D> model(Object key, Supplier<Model> modelSupplier);

View file

@ -42,4 +42,9 @@ public class BatchedMaterial<D extends InstanceData> implements Material<D> {
models.values()
.forEach(CPUInstancer::clear);
}
public void delete() {
models.values().forEach(CPUInstancer::delete);
models.clear();
}
}

View file

@ -67,6 +67,9 @@ public class BatchedMaterialGroup implements MaterialGroup {
}
public void delete() {
materials.values()
.forEach(BatchedMaterial::delete);
materials.clear();
}

View file

@ -64,11 +64,13 @@ public class BatchingEngine implements Engine {
@Override
public void delete() {
for (Map<RenderType, BatchedMaterialGroup> groups : layers.values()) {
groups.values().forEach(BatchedMaterialGroup::delete);
}
}
@Override
public void beginFrame(Camera info) {
}
@Override

View file

@ -73,4 +73,8 @@ public class CPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
public void notifyDirty() {
// noop
}
public void delete() {
modelData.delete();
}
}

View file

@ -104,6 +104,8 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
instanceVBO.delete();
vao.delete();
modelData.delete();
}
protected void renderSetup() {

View file

@ -43,6 +43,10 @@ public class InstancedMaterial<D extends InstanceData> implements Material<D> {
});
}
public Collection<GPUInstancer<D>> getAllInstancers() {
return models.values();
}
public int getInstanceCount() {
return models.values().stream().mapToInt(GPUInstancer::getInstanceCount).sum();
}
@ -57,11 +61,6 @@ public class InstancedMaterial<D extends InstanceData> implements Material<D> {
.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<D extends InstanceData> implements Material<D> {
.forEach(GPUInstancer::clear);
}
public Collection<GPUInstancer<D>> getAllInstancers() {
return models.values();
public void delete() {
models.values().forEach(GPUInstancer::delete);
models.clear();
}
}

View file

@ -116,7 +116,6 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
@Override
public void delete() {
for (Map<RenderType, InstancedMaterialGroup<P>> groups : layers.values()) {
groups.values().forEach(InstancedMaterialGroup::delete);
}
}

View file

@ -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.
*/

View file

@ -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) {

View file

@ -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);
}
}