Allow VertexViews to hold a memory owner

- Remove MemoryBlock parameter from SimpleQuadMesh constructors
This commit is contained in:
PepperCode1 2024-09-09 19:01:19 -07:00
parent 1a8ed8db28
commit 14ca1d3286
6 changed files with 36 additions and 13 deletions

View file

@ -2,12 +2,10 @@ package dev.engine_room.flywheel.api.model;
import java.util.List; import java.util.List;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.instance.InstancerProvider;
import org.joml.Vector4fc; import org.joml.Vector4fc;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.instance.InstancerProvider;
import dev.engine_room.flywheel.api.material.Material; import dev.engine_room.flywheel.api.material.Material;
public interface Model { public interface Model {

View file

@ -6,25 +6,21 @@ import org.joml.Vector4fc;
import dev.engine_room.flywheel.api.vertex.MutableVertexList; import dev.engine_room.flywheel.api.vertex.MutableVertexList;
import dev.engine_room.flywheel.api.vertex.VertexList; import dev.engine_room.flywheel.api.vertex.VertexList;
import dev.engine_room.flywheel.lib.memory.MemoryBlock;
public final class SimpleQuadMesh implements QuadMesh { public final class SimpleQuadMesh implements QuadMesh {
private final VertexList vertexList; private final VertexList vertexList;
// Unused but we need to hold on to a reference so the cleaner doesn't nuke us.
private final MemoryBlock data;
private final Vector4f boundingSphere; private final Vector4f boundingSphere;
@Nullable @Nullable
private final String descriptor; private final String descriptor;
public SimpleQuadMesh(VertexList vertexList, MemoryBlock data, @Nullable String descriptor) { public SimpleQuadMesh(VertexList vertexList, @Nullable String descriptor) {
this.vertexList = vertexList; this.vertexList = vertexList;
this.data = data;
boundingSphere = ModelUtil.computeBoundingSphere(vertexList); boundingSphere = ModelUtil.computeBoundingSphere(vertexList);
this.descriptor = descriptor; this.descriptor = descriptor;
} }
public SimpleQuadMesh(VertexList vertexList, MemoryBlock data) { public SimpleQuadMesh(VertexList vertexList) {
this(vertexList, data, null); this(vertexList, null);
} }
@Override @Override

View file

@ -41,7 +41,8 @@ final class MeshHelper {
vertexView.ptr(dstPtr); vertexView.ptr(dstPtr);
vertexView.vertexCount(vertexCount); vertexView.vertexCount(vertexCount);
vertexView.nativeMemoryOwner(dst);
return new SimpleQuadMesh(vertexView, dst, meshDescriptor); return new SimpleQuadMesh(vertexView, meshDescriptor);
} }
} }

View file

@ -37,7 +37,7 @@ public final class ModelPartConverter {
VertexView vertexView = new PosTexNormalVertexView(); VertexView vertexView = new PosTexNormalVertexView();
vertexView.load(data); vertexView.load(data);
return new SimpleQuadMesh(vertexView, data, "source=ModelPartConverter"); return new SimpleQuadMesh(vertexView, "source=ModelPartConverter");
} }
public static Mesh convert(ModelLayerLocation layer, @Nullable TextureAtlasSprite sprite, String... childPath) { public static Mesh convert(ModelLayerLocation layer, @Nullable TextureAtlasSprite sprite, String... childPath) {

View file

@ -1,8 +1,12 @@
package dev.engine_room.flywheel.lib.vertex; package dev.engine_room.flywheel.lib.vertex;
import org.jetbrains.annotations.Nullable;
public abstract class AbstractVertexView implements VertexView { public abstract class AbstractVertexView implements VertexView {
protected long ptr; protected long ptr;
protected int vertexCount; protected int vertexCount;
@Nullable
private Object nativeMemoryOwner;
@Override @Override
public long ptr() { public long ptr() {
@ -23,4 +27,15 @@ public abstract class AbstractVertexView implements VertexView {
public void vertexCount(int vertexCount) { public void vertexCount(int vertexCount) {
this.vertexCount = vertexCount; this.vertexCount = vertexCount;
} }
@Override
@Nullable
public final Object nativeMemoryOwner() {
return nativeMemoryOwner;
}
@Override
public final void nativeMemoryOwner(@Nullable Object owner) {
nativeMemoryOwner = owner;
}
} }

View file

@ -1,5 +1,6 @@
package dev.engine_room.flywheel.lib.vertex; package dev.engine_room.flywheel.lib.vertex;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.system.MemoryUtil; import org.lwjgl.system.MemoryUtil;
import dev.engine_room.flywheel.api.vertex.MutableVertexList; import dev.engine_room.flywheel.api.vertex.MutableVertexList;
@ -14,6 +15,17 @@ public interface VertexView extends MutableVertexList {
long stride(); long stride();
@Nullable
Object nativeMemoryOwner();
/**
* The memory referenced by this vertex view's pointer may be owned by another object, such that the memory is
* automatically freed when the other object becomes phantom reachable or is garbage collected. Use this method to
* ensure this vertex view retains a strong reference to the memory owner so this vertex view's pointer remains
* valid even when no other references to the memory owner are retained.
*/
void nativeMemoryOwner(@Nullable Object owner);
default void load(MemoryBlock data) { default void load(MemoryBlock data) {
long bytes = data.size(); long bytes = data.size();
long stride = stride(); long stride = stride();
@ -24,6 +36,7 @@ public interface VertexView extends MutableVertexList {
ptr(data.ptr()); ptr(data.ptr());
vertexCount(vertexCount); vertexCount(vertexCount);
nativeMemoryOwner(data);
} }
@Override @Override