Skip to content

Commit

Permalink
Fix multithread conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jun 8, 2024
1 parent b3f8bb1 commit bae5abc
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cn.zbx1425.mtrsteamloco.render.scripting;

import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder;
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.ModelCluster;
import cn.zbx1425.sowcerext.reuse.DrawScheduler;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;

public abstract class AbstractDrawCalls {

public static class ClusterDrawCall {
public ModelCluster model;
public DynamicModelHolder modelHolder;
public Matrix4f pose;

public ClusterDrawCall(ModelCluster model, Matrix4f pose) {
this.model = model;
this.pose = pose;
}

public ClusterDrawCall(DynamicModelHolder model, Matrix4f pose) {
this.modelHolder = model;
this.pose = pose;
}

public void commit(DrawScheduler drawScheduler, Matrix4f basePose, int light) {
Matrix4f finalPose = basePose.copy();
finalPose.multiply(pose);
if (model != null) {
drawScheduler.enqueue(model, finalPose, light);
} else {
ModelCluster model = modelHolder.getUploadedModel();
if (model != null) {
drawScheduler.enqueue(model, finalPose, light);
}
}
}
}

public static class PlaySoundCall {
public SoundEvent sound;
public Vector3f position;
public float volume;
public float pitch;

public PlaySoundCall(SoundEvent sound, Vector3f position, float volume, float pitch) {
this.sound = sound;
this.position = position;
this.volume = volume;
this.pitch = pitch;
}

public void commit(ClientLevel level, Matrix4f worldPose) {
Vector3f worldPos = worldPose.transform(position);
level.playLocalSound(worldPos.x(), worldPos.y(), worldPos.z(),
sound, SoundSource.BLOCKS,
volume, pitch, false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.zbx1425.mtrsteamloco.render.scripting.eyecandy;

import cn.zbx1425.mtrsteamloco.render.scripting.train.TrainDrawCalls;
import cn.zbx1425.mtrsteamloco.render.scripting.AbstractDrawCalls;
import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder;
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.ModelCluster;
Expand All @@ -13,7 +14,7 @@
import java.util.ArrayList;
import java.util.List;

public class EyeCandyDrawCalls {
public class EyeCandyDrawCalls extends AbstractDrawCalls {

private final List<ClusterDrawCall> drawList = new ArrayList<>();
private final List<PlaySoundCall> soundList = new ArrayList<>();
Expand All @@ -22,50 +23,27 @@ public void addModel(ModelCluster model, Matrix4f pose) {
drawList.add(new ClusterDrawCall(model, pose));
}

public void addModel(DynamicModelHolder model, Matrix4f pose) {
drawList.add(new ClusterDrawCall(model, pose));
}

public void addSound(SoundEvent sound, float volume, float pitch) {
soundList.add(new PlaySoundCall(sound, volume, pitch));
soundList.add(new PlaySoundCall(sound, Vector3f.ZERO, volume, pitch));
}

public void commit(DrawScheduler drawScheduler, Matrix4f basePose, int light) {
for (ClusterDrawCall clusterDrawCall : drawList) {
Matrix4f finalPose = basePose.copy();
finalPose.multiply(clusterDrawCall.pose);
drawScheduler.enqueue(clusterDrawCall.model, finalPose, light);
clusterDrawCall.commit(drawScheduler, basePose, light);
}
ClientLevel level = Minecraft.getInstance().level;
if (level == null) return;
for (PlaySoundCall playSoundCall : soundList) {
Vector3f worldPos = basePose.transform(Vector3f.ZERO);
level.playLocalSound(worldPos.x(), worldPos.y(), worldPos.z(),
playSoundCall.sound, SoundSource.BLOCKS,
playSoundCall.volume, playSoundCall.pitch, false);
playSoundCall.commit(level, basePose);
}
}

public void reset() {
drawList.clear();
soundList.clear();
}

private static class ClusterDrawCall {
public ModelCluster model;
public Matrix4f pose;

public ClusterDrawCall(ModelCluster model, Matrix4f pose) {
this.model = model;
this.pose = pose;
}
}

private static class PlaySoundCall {
public SoundEvent sound;
public float volume;
public float pitch;

public PlaySoundCall(SoundEvent sound, float volume, float pitch) {
this.sound = sound;
this.volume = volume;
this.pitch = pitch;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.zbx1425.mtrsteamloco.Main;
import cn.zbx1425.mtrsteamloco.block.BlockEyeCandy;
import cn.zbx1425.mtrsteamloco.render.scripting.AbstractScriptContext;
import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder;
import cn.zbx1425.sowcer.math.Matrices;
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcerext.model.ModelCluster;
Expand Down Expand Up @@ -43,7 +44,10 @@ public boolean isBearerAlive() {
}

public void drawModel(ModelCluster model, Matrices poseStack) {
if (model == null) return;
scriptResultWriting.addModel(model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

public void drawModel(DynamicModelHolder model, Matrices poseStack) {
scriptResultWriting.addModel(model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.zbx1425.mtrsteamloco.render.scripting.train;

import cn.zbx1425.mtrsteamloco.render.scripting.AbstractDrawCalls;
import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder;
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.ModelCluster;
Expand All @@ -15,19 +17,18 @@
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.phys.Vec3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TrainDrawCalls {
public class TrainDrawCalls extends AbstractDrawCalls {

private final List<ClusterDrawCall>[] carDrawLists;
private final List<ClusterDrawCall>[] connDrawLists;
private final ResourceLocation[] connStretchTextures;
private final List<PlayCarSoundCall>[] carSoundLists;
private final List<PlaySoundCall>[] carSoundLists;

@SuppressWarnings("unchecked")
public TrainDrawCalls(int carCount) {
Expand All @@ -44,39 +45,40 @@ public void addCarModel(int car, ModelCluster model, Matrix4f pose) {
carDrawLists[car].add(new ClusterDrawCall(model, pose));
}

public void addCarModel(int car, DynamicModelHolder model, Matrix4f pose) {
carDrawLists[car].add(new ClusterDrawCall(model, pose));
}

public void addCarSound(int car, SoundEvent sound, Vector3f position, float volume, float pitch) {
carSoundLists[car].add(new PlayCarSoundCall(sound, position, volume, pitch));
carSoundLists[car].add(new PlaySoundCall(sound, position, volume, pitch));
}

public void commitCar(int car, DrawScheduler drawScheduler, Matrix4f basePose, Matrix4f worldPose, int light) {
for (ClusterDrawCall clusterDrawCall : carDrawLists[car]) {
Matrix4f finalPose = basePose.copy();
finalPose.multiply(clusterDrawCall.pose);
drawScheduler.enqueue(clusterDrawCall.model, finalPose, light);
clusterDrawCall.commit(drawScheduler, basePose, light);
}
ClientLevel level = Minecraft.getInstance().level;
if (level == null) return;
for (PlayCarSoundCall playCarSoundCall : carSoundLists[car]) {
Vector3f worldPos = worldPose.transform(playCarSoundCall.position);
level.playLocalSound(worldPos.x(), worldPos.y(), worldPos.z(),
playCarSoundCall.sound, SoundSource.BLOCKS,
playCarSoundCall.volume, playCarSoundCall.pitch, false);
for (PlaySoundCall playSoundCall : carSoundLists[car]) {
playSoundCall.commit(level, worldPose);
}
}

public void addConnModel(int car, ModelCluster model, Matrix4f pose) {
connDrawLists[car].add(new ClusterDrawCall(model, pose));
}

public void addConnModel(int car, DynamicModelHolder model, Matrix4f pose) {
connDrawLists[car].add(new ClusterDrawCall(model, pose));
}

public void drawConnStretchTexture(int car, ResourceLocation texture) {
connStretchTextures[car] = texture;
}

public void commitConn(int car, DrawScheduler drawScheduler, Matrix4f basePose, int light) {
for (ClusterDrawCall clusterDrawCall : connDrawLists[car]) {
Matrix4f finalPose = basePose.copy();
finalPose.multiply(clusterDrawCall.pose);
drawScheduler.enqueue(clusterDrawCall.model, finalPose, light);
clusterDrawCall.commit(drawScheduler, basePose, light);
}
}

Expand Down Expand Up @@ -107,31 +109,8 @@ private static void drawTexture(PoseStack matrices, VertexConsumer vertexConsume
public void reset() {
for (List<ClusterDrawCall> list : carDrawLists) list.clear();
for (List<ClusterDrawCall> list : connDrawLists) list.clear();
for (List<PlayCarSoundCall> list : carSoundLists) list.clear();
for (List<PlaySoundCall> list : carSoundLists) list.clear();
Arrays.fill(connStretchTextures, null);
}

private static class ClusterDrawCall {
public ModelCluster model;
public Matrix4f pose;

public ClusterDrawCall(ModelCluster model, Matrix4f pose) {
this.model = model;
this.pose = pose;
}
}

private static class PlayCarSoundCall {
public SoundEvent sound;
public Vector3f position;
public float volume;
public float pitch;

public PlayCarSoundCall(SoundEvent sound, Vector3f position, float volume, float pitch) {
this.sound = sound;
this.position = position;
this.volume = volume;
this.pitch = pitch;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cn.zbx1425.mtrsteamloco.render.scripting.train;

import cn.zbx1425.mtrsteamloco.render.scripting.AbstractScriptContext;
import cn.zbx1425.mtrsteamloco.render.scripting.ScriptHolder;
import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder;
import cn.zbx1425.sowcer.math.Matrices;
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.ModelCluster;
import mtr.client.ClientData;
import mtr.client.TrainClientRegistry;
import mtr.data.TrainClient;
import mtr.render.TrainRendererBase;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
Expand Down Expand Up @@ -66,12 +64,18 @@ public void extraFinished() {
}

public void drawCarModel(ModelCluster model, int carIndex, Matrices poseStack) {
if (model == null) return;
scriptResultWriting.addCarModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

public void drawCarModel(DynamicModelHolder model, int carIndex, Matrices poseStack) {
scriptResultWriting.addCarModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

public void drawConnModel(ModelCluster model, int carIndex, Matrices poseStack) {
if (model == null) return;
scriptResultWriting.addConnModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

public void drawConnModel(DynamicModelHolder model, int carIndex, Matrices poseStack) {
scriptResultWriting.addConnModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public void uploadLater(RawModel rawModel) {
RenderSystem.recordRenderCall(() -> {
boolean needProtection = !GlStateTracker.isStateProtected;
if (needProtection) GlStateTracker.capture();
ModelCluster lastUploadedModel = uploadedModel;
uploadedModel = new ModelCluster(finalRawModel, ModelManager.DEFAULT_MAPPING);
if (lastUploadedModel != null) lastUploadedModel.close();
if (needProtection) GlStateTracker.restore();
});
}
Expand Down

0 comments on commit bae5abc

Please sign in to comment.