Skip to content

Commit

Permalink
First try
Browse files Browse the repository at this point in the history
- Implement instance hiding by deleting/stealing
- Work around instancer persistence by storing a recreation supplier in
  the instance handle
- Rework instancer ctors to just take an InstancerKey
- Parameterize InstanceHandle by I extends Instance so the steal method
  and the supplier can be safely assigned
  • Loading branch information
Jozufozu committed Sep 19, 2024
1 parent e1b594a commit 5a75fe9
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public interface InstanceHandle {
void setChanged();

void setDeleted();

void setVisible(boolean visible);

boolean isVisible();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.engine_room.flywheel.backend.engine;

import java.util.ArrayList;
import java.util.function.Supplier;

import org.jetbrains.annotations.Nullable;

Expand All @@ -13,26 +14,32 @@
public abstract class AbstractInstancer<I extends Instance> implements Instancer<I> {
public final InstanceType<I> type;
public final Environment environment;
private final Supplier<AbstractInstancer<I>> recreate;

// Lock for all instances, only needs to be used in methods that may run on the TaskExecutor.
protected final Object lock = new Object();
protected final ArrayList<I> instances = new ArrayList<>();
protected final ArrayList<InstanceHandleImpl> handles = new ArrayList<>();
protected final ArrayList<InstanceHandleImpl<I>> handles = new ArrayList<>();

protected final AtomicBitSet changed = new AtomicBitSet();
protected final AtomicBitSet deleted = new AtomicBitSet();

protected AbstractInstancer(InstanceType<I> type, Environment environment) {
this.type = type;
this.environment = environment;
protected AbstractInstancer(InstancerKey<I> key, Supplier<AbstractInstancer<I>> recreate) {
this.type = key.type();
this.environment = key.environment();
this.recreate = recreate;
}

@Override
public I createInstance() {
synchronized (lock) {
var i = instances.size();
var handle = new InstanceHandleImpl(this, i);
var handle = new InstanceHandleImpl<I>();
handle.instancer = this;
handle.recreate = recreate;
handle.index = i;
I instance = type.create(handle);
handle.instance = instance;

addLocked(instance, handle);
return instance;
Expand All @@ -47,12 +54,15 @@ public void stealInstance(@Nullable I instance) {

var instanceHandle = instance.handle();

if (!(instanceHandle instanceof InstanceHandleImpl handle)) {
if (!(instanceHandle instanceof InstanceHandleImpl<?>)) {
// UB: do nothing
return;
}

if (handle.instancer == this) {
// Should InstanceType have an isInstance method?
var handle = (InstanceHandleImpl<I>) instanceHandle;

if (handle.instancer == this && handle.visible) {
return;
}

Expand All @@ -65,19 +75,23 @@ public void stealInstance(@Nullable I instance) {
// is filtering deleted instances later, so is safe.
handle.setDeleted();

// Only lock now that we'll be mutating our state.
synchronized (lock) {
// Add the instance to this instancer.
handle.instancer = this;
handle.index = instances.size();
addLocked(instance, handle);
// Add the instance to this instancer.
handle.instancer = this;
handle.recreate = recreate;

if (handle.visible) {
// Only lock now that we'll be mutating our state.
synchronized (lock) {
handle.index = instances.size();
addLocked(instance, handle);
}
}
}

/**
* Calls must be synchronized on {@link #lock}.
*/
private void addLocked(I instance, InstanceHandleImpl handle) {
private void addLocked(I instance, InstanceHandleImpl<I> handle) {
instances.add(instance);
handles.add(handle);
changed.set(handle.index);
Expand Down Expand Up @@ -163,7 +177,7 @@ protected void setRangeChanged(int start, int end) {
* Clear all instances without freeing resources.
*/
public void clear() {
for (InstanceHandleImpl handle : handles) {
for (InstanceHandleImpl<I> handle : handles) {
// Only clear instances that belong to this instancer.
// If one of these handles was stolen by another instancer,
// clearing it here would cause significant visual artifacts and instance leaks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import dev.engine_room.flywheel.api.backend.Engine;
import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.instance.Instancer;
import dev.engine_room.flywheel.api.model.Model;
import dev.engine_room.flywheel.api.visualization.VisualType;
import dev.engine_room.flywheel.backend.FlwBackend;
Expand All @@ -36,9 +35,13 @@ public abstract class DrawManager<N extends AbstractInstancer<?>> {
*/
protected final Queue<UninitializedInstancer<N, ?>> initializationQueue = new ConcurrentLinkedQueue<>();

public <I extends Instance> AbstractInstancer<I> getInstancer(Environment environment, InstanceType<I> type, Model model, VisualType visualType, int bias) {
return getInstancer(new InstancerKey<>(environment, type, model, visualType, bias));
}

@SuppressWarnings("unchecked")
public <I extends Instance> Instancer<I> getInstancer(Environment environment, InstanceType<I> type, Model model, VisualType visualType, int bias) {
return (Instancer<I>) instancers.computeIfAbsent(new InstancerKey<>(environment, type, model, visualType, bias), this::createAndDeferInit);
public <I extends Instance> AbstractInstancer<I> getInstancer(InstancerKey<I> key) {
return (AbstractInstancer<I>) instancers.computeIfAbsent(key, this::createAndDeferInit);
}

public void flush(LightStorage lightStorage, EnvironmentStorage environmentStorage) {
Expand Down Expand Up @@ -94,8 +97,8 @@ private static boolean checkAndWarnEmptyModel(Model model) {
return false;
}

protected static <I extends AbstractInstancer<?>> Map<GroupKey<?>, Int2ObjectMap<List<Pair<I, InstanceHandleImpl>>>> doCrumblingSort(Class<I> clazz, List<Engine.CrumblingBlock> crumblingBlocks) {
Map<GroupKey<?>, Int2ObjectMap<List<Pair<I, InstanceHandleImpl>>>> byType = new HashMap<>();
protected static <I extends AbstractInstancer<?>> Map<GroupKey<?>, Int2ObjectMap<List<Pair<I, InstanceHandleImpl<?>>>>> doCrumblingSort(Class<I> clazz, List<Engine.CrumblingBlock> crumblingBlocks) {
Map<GroupKey<?>, Int2ObjectMap<List<Pair<I, InstanceHandleImpl<?>>>>> byType = new HashMap<>();
for (Engine.CrumblingBlock block : crumblingBlocks) {
int progress = block.progress();

Expand All @@ -107,7 +110,7 @@ protected static <I extends AbstractInstancer<?>> Map<GroupKey<?>, Int2ObjectMap
// Filter out instances that weren't created by this engine.
// If all is well, we probably shouldn't take the `continue`
// branches but better to do checked casts.
if (!(instance.handle() instanceof InstanceHandleImpl impl)) {
if (!(instance.handle() instanceof InstanceHandleImpl<?> impl)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package dev.engine_room.flywheel.backend.engine;

import java.util.function.Supplier;

import org.jetbrains.annotations.UnknownNullability;

import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.instance.InstanceHandle;

public class InstanceHandleImpl implements InstanceHandle {
public AbstractInstancer<?> instancer;
public class InstanceHandleImpl<I extends Instance> implements InstanceHandle {
@UnknownNullability
public AbstractInstancer<I> instancer;
@UnknownNullability
public I instance;
@UnknownNullability
public Supplier<AbstractInstancer<I>> recreate;
public boolean visible = true;
public int index;

public InstanceHandleImpl(AbstractInstancer<?> instancer, int index) {
this.instancer = instancer;
this.index = index;
}

@Override
public void setChanged() {
instancer.notifyDirty(index);
Expand All @@ -23,6 +29,27 @@ public void setDeleted() {
clear();
}

@Override
public void setVisible(boolean visible) {
if (this.visible == visible) {
return;
}

this.visible = visible;

if (visible) {
recreate.get().stealInstance(instance);
} else {
instancer.notifyRemoval(index);
clear();
}
}

@Override
public boolean isVisible() {
return visible;
}

public void clear() {
index = -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IndirectDrawManager(IndirectPrograms programs) {

@Override
protected <I extends Instance> IndirectInstancer<?> create(InstancerKey<I> key) {
return new IndirectInstancer<>(key.type(), key.environment(), key.model());
return new IndirectInstancer<>(key, () -> getInstancer(key));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import org.jetbrains.annotations.UnknownNullability;
import org.joml.Vector4fc;
import org.lwjgl.system.MemoryUtil;

import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.instance.InstanceWriter;
import dev.engine_room.flywheel.api.model.Model;
import dev.engine_room.flywheel.backend.engine.AbstractInstancer;
import dev.engine_room.flywheel.backend.engine.embed.Environment;
import dev.engine_room.flywheel.backend.engine.InstancerKey;
import dev.engine_room.flywheel.backend.util.AtomicBitSet;
import dev.engine_room.flywheel.lib.math.MoreMath;

Expand All @@ -29,12 +28,12 @@ public class IndirectInstancer<I extends Instance> extends AbstractInstancer<I>
private int modelIndex = -1;
private int baseInstance = -1;

public IndirectInstancer(InstanceType<I> type, Environment environment, Model model) {
super(type, environment);
public IndirectInstancer(InstancerKey<I> key, Supplier<AbstractInstancer<I>> recreate) {
super(key, recreate);
instanceStride = MoreMath.align4(type.layout()
.byteSize());
writer = this.type.writer();
boundingSphere = model.boundingSphere();
boundingSphere = key.model().boundingSphere();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void delete() {

@Override
protected <I extends Instance> InstancedInstancer<I> create(InstancerKey<I> key) {
return new InstancedInstancer<>(key.type(), key.environment());
return new InstancedInstancer<>(key, () -> getInstancer(key));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import org.jetbrains.annotations.Nullable;

import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.api.instance.InstanceWriter;
import dev.engine_room.flywheel.backend.engine.AbstractInstancer;
import dev.engine_room.flywheel.backend.engine.embed.Environment;
import dev.engine_room.flywheel.backend.engine.InstancerKey;
import dev.engine_room.flywheel.backend.gl.TextureBuffer;
import dev.engine_room.flywheel.backend.gl.buffer.GlBuffer;
import dev.engine_room.flywheel.backend.gl.buffer.GlBufferUsage;
Expand All @@ -25,8 +25,8 @@ public class InstancedInstancer<I extends Instance> extends AbstractInstancer<I>

private final List<InstancedDraw> draws = new ArrayList<>();

public InstancedInstancer(InstanceType<I> type, Environment environment) {
super(type, environment);
public InstancedInstancer(InstancerKey<I> key, Supplier<AbstractInstancer<I>> recreate) {
super(key, recreate);
var layout = type.layout();
// Align to one texel in the texture buffer
instanceStride = MoreMath.align16(layout.byteSize());
Expand Down

0 comments on commit 5a75fe9

Please sign in to comment.