mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2025-01-27 21:37:56 +01:00
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:
parent
f6d5ed01e7
commit
f56d0f9024
11 changed files with 36 additions and 11 deletions
|
@ -19,6 +19,7 @@ public interface Material<D extends InstanceData> {
|
||||||
*
|
*
|
||||||
* @param key An object that uniquely identifies the model.
|
* @param key An object that uniquely identifies the model.
|
||||||
* @param modelSupplier A factory that creates the Model that you want to render.
|
* @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.
|
* @return An instancer for the given model, capable of rendering many copies for little cost.
|
||||||
*/
|
*/
|
||||||
Instancer<D> model(Object key, Supplier<Model> modelSupplier);
|
Instancer<D> model(Object key, Supplier<Model> modelSupplier);
|
||||||
|
|
|
@ -42,4 +42,9 @@ public class BatchedMaterial<D extends InstanceData> implements Material<D> {
|
||||||
models.values()
|
models.values()
|
||||||
.forEach(CPUInstancer::clear);
|
.forEach(CPUInstancer::clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void delete() {
|
||||||
|
models.values().forEach(CPUInstancer::delete);
|
||||||
|
models.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,9 @@ public class BatchedMaterialGroup implements MaterialGroup {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete() {
|
public void delete() {
|
||||||
|
materials.values()
|
||||||
|
.forEach(BatchedMaterial::delete);
|
||||||
|
|
||||||
materials.clear();
|
materials.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,11 +64,13 @@ public class BatchingEngine implements Engine {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete() {
|
public void delete() {
|
||||||
|
for (Map<RenderType, BatchedMaterialGroup> groups : layers.values()) {
|
||||||
|
groups.values().forEach(BatchedMaterialGroup::delete);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beginFrame(Camera info) {
|
public void beginFrame(Camera info) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -73,4 +73,8 @@ public class CPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
|
||||||
public void notifyDirty() {
|
public void notifyDirty() {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void delete() {
|
||||||
|
modelData.delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,6 +104,8 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
|
||||||
|
|
||||||
instanceVBO.delete();
|
instanceVBO.delete();
|
||||||
vao.delete();
|
vao.delete();
|
||||||
|
|
||||||
|
modelData.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void renderSetup() {
|
protected void renderSetup() {
|
||||||
|
|
|
@ -43,6 +43,10 @@ public class InstancedMaterial<D extends InstanceData> implements Material<D> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Collection<GPUInstancer<D>> getAllInstancers() {
|
||||||
|
return models.values();
|
||||||
|
}
|
||||||
|
|
||||||
public int getInstanceCount() {
|
public int getInstanceCount() {
|
||||||
return models.values().stream().mapToInt(GPUInstancer::getInstanceCount).sum();
|
return models.values().stream().mapToInt(GPUInstancer::getInstanceCount).sum();
|
||||||
}
|
}
|
||||||
|
@ -57,11 +61,6 @@ public class InstancedMaterial<D extends InstanceData> implements Material<D> {
|
||||||
.allMatch(GPUInstancer::isEmpty);
|
.allMatch(GPUInstancer::isEmpty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete() {
|
|
||||||
models.values().forEach(GPUInstancer::delete);
|
|
||||||
models.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all instance data without freeing resources.
|
* Clear all instance data without freeing resources.
|
||||||
*/
|
*/
|
||||||
|
@ -70,7 +69,8 @@ public class InstancedMaterial<D extends InstanceData> implements Material<D> {
|
||||||
.forEach(GPUInstancer::clear);
|
.forEach(GPUInstancer::clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<GPUInstancer<D>> getAllInstancers() {
|
public void delete() {
|
||||||
return models.values();
|
models.values().forEach(GPUInstancer::delete);
|
||||||
|
models.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,6 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
|
||||||
@Override
|
@Override
|
||||||
public void delete() {
|
public void delete() {
|
||||||
for (Map<RenderType, InstancedMaterialGroup<P>> groups : layers.values()) {
|
for (Map<RenderType, InstancedMaterialGroup<P>> groups : layers.values()) {
|
||||||
|
|
||||||
groups.values().forEach(InstancedMaterialGroup::delete);
|
groups.values().forEach(InstancedMaterialGroup::delete);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,10 @@ public class ArrayModelRenderer {
|
||||||
this.model = model;
|
this.model = model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Model getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders this model, checking first if there is anything to render.
|
* Renders this model, checking first if there is anything to render.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package com.jozufozu.flywheel.core.hardcoded;
|
package com.jozufozu.flywheel.core.hardcoded;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.lwjgl.system.MemoryUtil;
|
||||||
|
|
||||||
import com.jozufozu.flywheel.api.vertex.VertexList;
|
import com.jozufozu.flywheel.api.vertex.VertexList;
|
||||||
import com.jozufozu.flywheel.api.vertex.VertexType;
|
import com.jozufozu.flywheel.api.vertex.VertexType;
|
||||||
import com.jozufozu.flywheel.core.Formats;
|
import com.jozufozu.flywheel.core.Formats;
|
||||||
|
@ -26,12 +29,14 @@ public class ModelPart implements Model {
|
||||||
this.vertices = vertices;
|
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) {
|
for (PartBuilder.CuboidBuilder cuboid : cuboids) {
|
||||||
cuboid.buffer(writer);
|
cuboid.buffer(writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
reader = writer.intoReader();
|
reader = writer.intoReader();
|
||||||
|
MemoryUtil.memFree(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PartBuilder builder(String name, int sizeU, int sizeV) {
|
public static PartBuilder builder(String name, int sizeU, int sizeV) {
|
||||||
|
|
|
@ -81,6 +81,6 @@ public final class WorldModelBuilder implements Bufferable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public BlockModel intoMesh(String name) {
|
public BlockModel intoMesh(String name) {
|
||||||
return new BlockModel(ModelUtil.getBufferBuilder(this), name);
|
return new BlockModel(this, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue