Skip to content

Commit

Permalink
Fix random crashing with Sodium
Browse files Browse the repository at this point in the history
Closes #29 (at least in my testing)
  • Loading branch information
Motschen committed Nov 21, 2022
1 parent 24f654f commit 2edfac7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import me.jellysquid.mods.sodium.client.model.vertex.type.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.*;
import me.jellysquid.mods.sodium.client.render.chunk.data.ChunkRenderBounds;
import me.jellysquid.mods.sodium.client.render.chunk.passes.BlockRenderPass;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -18,11 +19,13 @@

import java.util.List;

@Mixin(value = RegionChunkRenderer.class, remap = false)
@Mixin(value = RegionChunkRenderer.class, remap = false, priority = 1100)
public abstract class MixinRegionChunkRenderer extends ShaderChunkRenderer {
@Shadow @Final private boolean isBlockFaceCullingEnabled;
@Shadow protected abstract void addDrawCall(ElementRange part, long baseIndexPointer, int baseVertexIndex);
@Unique private boolean doFix = false;
@Unique private ChunkCameraContext camera;
@Unique private ChunkRenderBounds bounds;

public MixinRegionChunkRenderer(RenderDevice device, ChunkVertexType vertexType) {
super(device, vertexType);
Expand All @@ -31,14 +34,37 @@ public MixinRegionChunkRenderer(RenderDevice device, ChunkVertexType vertexType)
@Inject(method = "buildDrawBatches", at = @At(value = "INVOKE", target = "Lme/jellysquid/mods/sodium/client/render/chunk/RenderSection;getBounds()Lme/jellysquid/mods/sodium/client/render/chunk/data/ChunkRenderBounds;", ordinal = 0))
public void cullleaves$shouldApplyTransparentBlockFaceCullingFix(List<RenderSection> sections, BlockRenderPass pass, ChunkCameraContext camera, CallbackInfoReturnable<Boolean> cir) {
doFix = pass.getAlphaCutoff() != 0 && CullLeavesConfig.enabled && CullLeavesConfig.sodiumBlockFaceCullingFix && this.isBlockFaceCullingEnabled;
this.camera = camera;
}
@Redirect(method = "buildDrawBatches", at = @At(value = "INVOKE", target = "Lme/jellysquid/mods/sodium/client/render/chunk/RenderSection;getBounds()Lme/jellysquid/mods/sodium/client/render/chunk/data/ChunkRenderBounds;", ordinal = 0))
public ChunkRenderBounds cullleaves$getChunkRenderBounds(RenderSection instance) {
bounds = instance.getBounds();
return bounds;
}
@Redirect(method = "buildDrawBatches", at = @At(value = "INVOKE", target = "Lme/jellysquid/mods/sodium/client/render/chunk/ChunkGraphicsState;getModelPart(Lme/jellysquid/mods/sodium/client/model/quad/properties/ModelQuadFacing;)Lme/jellysquid/mods/sodium/client/gl/util/ElementRange;", ordinal = 0))
public ElementRange cullleaves$fixTransparentBlockFaceCulling(ChunkGraphicsState state, ModelQuadFacing facing) {
if (doFix) {
long indexOffset = state.getIndexSegment().getOffset();
int baseVertex = state.getVertexSegment().getOffset() / this.vertexFormat.getStride();
for (ModelQuadFacing direction : ModelQuadFacing.DIRECTIONS) {
this.addDrawCall(state.getModelPart(direction), indexOffset, baseVertex);
if (this.isBlockFaceCullingEnabled) {
if (camera.posY <= bounds.y1) {
this.addDrawCall(state.getModelPart(ModelQuadFacing.UP), indexOffset, baseVertex);
}
if (camera.posY >= bounds.y2) {
this.addDrawCall(state.getModelPart(ModelQuadFacing.DOWN), indexOffset, baseVertex);
}
if (camera.posX <= bounds.x1) {
this.addDrawCall(state.getModelPart(ModelQuadFacing.EAST), indexOffset, baseVertex);
}
if (camera.posX >= bounds.x2) {
this.addDrawCall(state.getModelPart(ModelQuadFacing.WEST), indexOffset, baseVertex);
}
if (camera.posZ <= bounds.z1) {
this.addDrawCall(state.getModelPart(ModelQuadFacing.SOUTH), indexOffset, baseVertex);
}
if (camera.posZ >= bounds.z2) {
this.addDrawCall(state.getModelPart(ModelQuadFacing.NORTH), indexOffset, baseVertex);
}
}
}
return state.getModelPart(ModelQuadFacing.UNASSIGNED);
Expand Down
2 changes: 1 addition & 1 deletion forge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ side = "CLIENT"
modId = "midnightlib"
mandatory = true
versionRange = "[1.0.0,)"
ordering = "NONE"
ordering = "BEFORE"
side = "CLIENT"
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minecraft_version=1.19.2
enabled_platforms=quilt,fabric,forge

archives_base_name=cullleaves
mod_version=3.0.0
mod_version=3.0.1
maven_group=eu.midnightdust

architectury_version=6.2.43
Expand Down

0 comments on commit 2edfac7

Please sign in to comment.