Skip to content

Commit

Permalink
避免在API类被垃圾回收时发生的原生代码报错
Browse files Browse the repository at this point in the history
  • Loading branch information
ChloePrime committed Mar 1, 2024
1 parent bfe3e4a commit 2e0b78f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import Effekseer.swig.EffekseerBackendCore;

import java.io.Closeable;

/**
* @author ChloePrime
*/
@SuppressWarnings("unused")
public class Effekseer implements Closeable {
public class Effekseer extends SafeFinalized<EffekseerBackendCore> {
public Effekseer() {
this(new EffekseerBackendCore());
}

public static boolean init() {
return EffekseerBackendCore.InitializeWithOpenGL();
Expand All @@ -31,5 +32,10 @@ public final EffekseerBackendCore getImpl() {
return impl;
}

private final EffekseerBackendCore impl = new EffekseerBackendCore();
protected Effekseer(EffekseerBackendCore impl) {
super(impl, EffekseerBackendCore::delete);
this.impl = impl;
}

private final EffekseerBackendCore impl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import Effekseer.swig.EffekseerEffectCore;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

/**
* @author ChloePrime
*/
@SuppressWarnings("unused")
public class EffekseerEffect implements Closeable {
public class EffekseerEffect extends SafeFinalized<EffekseerEffectCore> {
protected final EffekseerEffectCore impl;

private boolean isLoaded = false;

public EffekseerEffect() {
impl = new EffekseerEffectCore();
this(new EffekseerEffectCore());
}

@Override
Expand Down Expand Up @@ -141,4 +140,9 @@ public int maxTerm() {
public final EffekseerEffectCore getImpl() {
return impl;
}

protected EffekseerEffect(EffekseerEffectCore impl) {
super(impl, EffekseerEffectCore::delete);
this.impl = impl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import Effekseer.swig.EffekseerManagerCore;
import com.mojang.math.Matrix4f;

import java.io.Closeable;
import java.nio.FloatBuffer;

/**
* @author ChloePrime
*/
@SuppressWarnings("unused")
public class EffekseerManager implements Closeable {
public class EffekseerManager extends SafeFinalized<EffekseerManagerCore> {
public EffekseerManager() {
this(new EffekseerManagerCore());
}
Expand Down Expand Up @@ -126,6 +125,7 @@ public final EffekseerManagerCore getImpl() {
}

protected EffekseerManager(EffekseerManagerCore impl) {
super(impl, EffekseerManagerCore::delete);
this.impl = impl;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mod.chloeprime.aaaparticles.api.client.effekseer;

import net.minecraft.client.Minecraft;

import java.io.Closeable;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

/**
* 避免GC线程回收Effekseer对象导致nv驱动报错
*/
abstract class SafeFinalized<T> implements Closeable {
private static final Set<Object> KEEPER = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final AtomicReference<T> kept;
private final Consumer<T> closer;

protected SafeFinalized(T kept, Consumer<T> closer) {
this.kept = new AtomicReference<>(kept);
this.closer = closer;
KEEPER.add(kept);
}

@Override
@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();
var kept = this.kept.get();
if (kept != null) {
Minecraft.getInstance().tell(this::close);
}
}

@Override
public void close() {
T removed = this.kept.getAndSet(null);
if (removed == null) {
return;
}
try {
closer.accept(removed);
} finally {
KEEPER.remove(removed);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class AAAParticlesClient
{
public static final boolean DEBUG_ENABLED = true;
public static final boolean DEBUG_ENABLED = false;

public static void init() {
installNativeLibrary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import dev.architectury.event.EventResult;
import dev.architectury.event.events.client.ClientRawInputEvent;
import mod.chloeprime.aaaparticles.AAAParticles;
import mod.chloeprime.aaaparticles.api.client.effekseer.EffekseerEffect;
import mod.chloeprime.aaaparticles.api.common.AAALevel;
import mod.chloeprime.aaaparticles.api.common.ParticleEmitterInfo;
import mod.chloeprime.aaaparticles.client.render.EffekRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.LogManager;

import java.lang.ref.WeakReference;

/**
* @author ChloePrime
*/
Expand All @@ -35,7 +37,10 @@ public static void keyPressed0(Minecraft client, int keyCode, int scanCode, int
return;
}
if (keyCode == InputConstants.KEY_M) {
EffekRenderer.bPrintMatrix = true;
var effect = new WeakReference<>(new EffekseerEffect());
while (effect.get() != null) {
System.gc();
}
return;
}
if (keyCode != DEBUG_KEY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
* @author ChloePrime
*/
public class EffekRenderer {
public static boolean bPrintMatrix;

private static final FloatBuffer CAMERA_TRANSFORM_BUFFER = BufferUtils.createFloatBuffer(16);
private static final FloatBuffer PROJECTION_BUFFER = BufferUtils.createFloatBuffer(16);
private static final AtomicBoolean INIT = new AtomicBoolean();
Expand All @@ -45,7 +43,6 @@ public static void onRenderWorldLast(float partialTick, PoseStack pose, Matrix4f

CAMERA_TRANSFORM_BUFFER.clear();
PROJECTION_BUFFER.clear();
bPrintMatrix = false;
}

private static final float[] CAMERA_TRANSFORM_DATA = new float[16];
Expand Down

0 comments on commit 2e0b78f

Please sign in to comment.