Skip to content

Commit

Permalink
applied ruby's patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePlasticPotato committed Jan 29, 2024
1 parent 93b62e4 commit 9262503
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 274 deletions.
2 changes: 1 addition & 1 deletion common/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ port_lib_modules = accessors,base,entity,extensions,fake_players,networking,obj_
cloth_config_version = 11.1.106

# https://modrinth.com/mod/sodium/versions
sodium_version = mc1.20-0.4.10
sodium_version = mc1.20.1-0.5.3
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.valkyrienskies.mod.compat;

import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer;
import me.jellysquid.mods.sodium.client.render.chunk.map.ChunkStatus;
import me.jellysquid.mods.sodium.client.render.chunk.map.ChunkTrackerHolder;
import net.minecraft.client.multiplayer.ClientLevel;
import org.valkyrienskies.mod.mixin.ValkyrienCommonMixinConfigPlugin;

public class SodiumCompat {

public static void onChunkAdded(final int x, final int z) {
public static void onChunkAdded(final ClientLevel level, final int x, final int z) {
if (ValkyrienCommonMixinConfigPlugin.getVSRenderer() == VSRenderer.SODIUM) {
SodiumWorldRenderer.instance().onChunkAdded(x, z);
ChunkTrackerHolder.get(level).onChunkStatusAdded(x, z, ChunkStatus.FLAG_HAS_BLOCK_DATA);
}
}

public static void onChunkRemoved(final int x, final int z) {
public static void onChunkRemoved(final ClientLevel level, final int x, final int z) {
if (ValkyrienCommonMixinConfigPlugin.getVSRenderer() == VSRenderer.SODIUM) {
SodiumWorldRenderer.instance().onChunkRemoved(x, z);
ChunkTrackerHolder.get(level).onChunkStatusRemoved(x, z, ChunkStatus.FLAG_HAS_BLOCK_DATA);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ public String getRefMapperConfig() {

@Override
public boolean shouldApplyMixin(final String s, final String mixinClassName) {
final VSRenderer renderer = getVSRenderer();
if (mixinClassName.contains("org.valkyrienskies.mod.mixin.mod_compat.sodium")) {
return getVSRenderer() == VSRenderer.SODIUM;
return renderer == VSRenderer.SODIUM;
}
if (mixinClassName.contains("org.valkyrienskies.mod.mixin.mod_compat.optifine_vanilla")) {
return getVSRenderer() != VSRenderer.SODIUM;
return renderer == VSRenderer.VANILLA || renderer == VSRenderer.OPTIFINE;
}
if (mixinClassName.contains("org.valkyrienskies.mod.mixin.mod_compat.vanilla_renderer")) {
return getVSRenderer() == VSRenderer.VANILLA;
return renderer == VSRenderer.VANILLA;
}
if (mixinClassName.contains("org.valkyrienskies.mod.mixin.mod_compat.optifine")) {
return getVSRenderer() == VSRenderer.OPTIFINE;
return renderer == VSRenderer.OPTIFINE;
}
if (mixinClassName.contains("org.valkyrienskies.mod.mixin.feature.render_pathfinding")) {
return PATH_FINDING_DEBUG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void preLoadChunkFromPacket(final int x, final int z,
}

this.level.onChunkLoaded(new ChunkPos(x, z));
SodiumCompat.onChunkAdded(x, z);
SodiumCompat.onChunkAdded(this.level, x, z);
cir.setReturnValue(worldChunk);
}
}
Expand All @@ -84,7 +84,7 @@ public void preUnload(final int chunkX, final int chunkZ, final CallbackInfo ci)
((IVSViewAreaMethods) ((LevelRendererAccessor) ((ClientLevelAccessor) level).getLevelRenderer()).getViewArea())
.unloadChunk(chunkX, chunkZ);
}
SodiumCompat.onChunkRemoved(chunkX, chunkZ);
SodiumCompat.onChunkRemoved(this.level, chunkX, chunkZ);
ci.cancel();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
package org.valkyrienskies.mod.mixin.mod_compat.sodium;

import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import me.jellysquid.mods.sodium.client.render.chunk.ChunkTracker;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;
import me.jellysquid.mods.sodium.client.render.chunk.map.ChunkStatus;
import me.jellysquid.mods.sodium.client.render.chunk.map.ChunkTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

@Mixin(value = ChunkTracker.class, remap = false)
@Mixin(ChunkTracker.class)
public class MixinChunkTracker {

@Shadow
@Final
private Long2IntOpenHashMap chunkStatus;

@Shadow
@Final
private LongOpenHashSet chunkReady;

@Shadow
@Final
private LongSet unloadQueue;

@Shadow
@Final
private Long2IntOpenHashMap single;
private LongSet loadQueue;

@Inject(method = "updateMerged", at = @At("HEAD"), cancellable = true, remap = false)
public void beforeUpdateMerged(final int x, final int z, final CallbackInfo ci) {
final Level level = Minecraft.getInstance().level;
if (level == null || !VSGameUtilsKt.isChunkInShipyard(level, x, z)) {
return;
}

@Inject(method = "onLightDataAdded", at = @At("HEAD"), cancellable = true)
private void cancelDataLight(final int x, final int z, final CallbackInfo ci) {
final long key = ChunkPos.asLong(x, z);
final int existingFlags = this.single.get(key);
if ((existingFlags & 1) == 0) {
ci.cancel(); // Cancel instead of throwing an error for now

final int flags = this.chunkStatus.get(key);

if (flags == ChunkStatus.FLAG_ALL) {
if (this.chunkReady.add(key) && !this.unloadQueue.remove(key)) {
this.loadQueue.add(key);
}
} else {
if (this.chunkReady.remove(key) && !this.loadQueue.remove(key)) {
this.unloadQueue.add(key);
}
}

ci.cancel();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package org.valkyrienskies.mod.mixin.mod_compat.sodium;

import it.unimi.dsi.fastutil.PriorityQueue;
import it.unimi.dsi.fastutil.objects.ObjectList;
import java.util.ArrayDeque;
import java.util.Map;
import java.util.WeakHashMap;
import me.jellysquid.mods.sodium.client.render.chunk.ChunkRenderList;
import me.jellysquid.mods.sodium.client.render.chunk.ChunkUpdateType;
import me.jellysquid.mods.sodium.client.render.chunk.RegionChunkRenderer;
import me.jellysquid.mods.sodium.client.render.chunk.RenderSection;
import me.jellysquid.mods.sodium.client.render.chunk.RenderSectionManager;
import me.jellysquid.mods.sodium.client.render.chunk.data.ChunkRenderBounds;
import me.jellysquid.mods.sodium.client.util.frustum.Frustum;
import me.jellysquid.mods.sodium.client.render.chunk.lists.SortedRenderLists;
import me.jellysquid.mods.sodium.client.render.chunk.lists.VisibleChunkCollector;
import me.jellysquid.mods.sodium.client.render.viewport.Viewport;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import org.joml.primitives.AABBd;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.ClientShip;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
Expand All @@ -31,46 +27,29 @@
public abstract class MixinRenderSectionManager implements RenderSectionManagerDuck {

@Unique
private final WeakHashMap<ClientShip, ChunkRenderList> shipRenderLists = new WeakHashMap<>();
private final WeakHashMap<ClientShip, SortedRenderLists> shipRenderLists = new WeakHashMap<>();

@Override
public WeakHashMap<ClientShip, ChunkRenderList> getShipRenderLists() {
public WeakHashMap<ClientShip, SortedRenderLists> vs_getShipRenderLists() {
return shipRenderLists;
}

@Shadow
@Final
private ClientLevel world;
@Shadow
@Final
private ObjectList<RenderSection> tickableChunks;
@Shadow
@Final
private RegionChunkRenderer chunkRenderer;
@Shadow
@Final
private Map<ChunkUpdateType, PriorityQueue<RenderSection>> rebuildQueues;
@Shadow
private float cameraX;
@Shadow
private float cameraY;
@Shadow
private float cameraZ;

@Shadow
protected abstract RenderSection getRenderSection(int x, int y, int z);

@Shadow
protected abstract void addEntitiesToRenderLists(RenderSection render);

@Shadow
@Final
private static double NEARBY_CHUNK_DISTANCE;
private Map<ChunkUpdateType, ArrayDeque<RenderSection>> rebuildLists;

@Inject(at = @At("TAIL"), method = "iterateChunks")
private void afterIterateChunks(final Camera camera, final Frustum frustum, final int frame,
@Inject(at = @At("TAIL"), method = "createTerrainRenderList")
private void afterIterateChunks(final Camera camera, final Viewport viewport, final int frame,
final boolean spectator, final CallbackInfo ci) {
for (final ClientShip ship : VSGameUtilsKt.getShipObjectWorld(Minecraft.getInstance()).getLoadedShips()) {
final VisibleChunkCollector collector = new VisibleChunkCollector(frame);

ship.getActiveChunksSet().forEach((x, z) -> {
for (int y = world.getMinSection(); y < world.getMaxSection(); y++) {
final RenderSection section = getRenderSection(x, y, z);
Expand All @@ -79,51 +58,21 @@ private void afterIterateChunks(final Camera camera, final Frustum frustum, fina
continue;
}

if (section.getPendingUpdate() != null) {
final PriorityQueue<RenderSection> queue = this.rebuildQueues.get(section.getPendingUpdate());
if (queue.size() < (2 << 4) - 1) {
queue.enqueue(section);
}
}

final ChunkRenderBounds b = section.getBounds();
final AABBd b2 = new AABBd(b.x1 - 6e-1, b.y1 - 6e-1, b.z1 - 6e-1,
b.x2 + 6e-1, b.y2 + 6e-1, b.z2 + 6e-1)
.transform(ship.getRenderTransform().getShipToWorld());

if (section.isEmpty() ||
!frustum.isBoxVisible((float) b2.minX, (float) b2.minY, (float) b2.minZ,
(float) b2.maxX, (float) b2.maxY, (float) b2.maxZ)) {
continue;
}

shipRenderLists.computeIfAbsent(ship, k -> new ChunkRenderList()).add(section);

if (section.isTickable()) {
tickableChunks.add(section);
}

addEntitiesToRenderLists(section);
collector.accept(section);
}
});
}
}

@Redirect(
at = @At(
value = "INVOKE",
target = "Lme/jellysquid/mods/sodium/client/render/chunk/RenderSectionManager;isChunkPrioritized(Lme/jellysquid/mods/sodium/client/render/chunk/RenderSection;)Z"
),
method = "scheduleRebuild"
)
private boolean redirectIsChunkPrioritized(final RenderSectionManager instance, final RenderSection render) {
return VSGameUtilsKt.squaredDistanceBetweenInclShips(world,
render.getOriginX() + 8, render.getOriginY() + 8, render.getOriginZ() + 8,
this.cameraX, this.cameraY, this.cameraZ) <= NEARBY_CHUNK_DISTANCE;
shipRenderLists.put(ship, collector.createRenderLists());

// merge rebuild lists
for (final var entry : collector.getRebuildLists().entrySet()) {
this.rebuildLists.get(entry.getKey()).addAll(entry.getValue());
}
}
}

@Inject(at = @At("TAIL"), method = "resetLists")
@Inject(at = @At("TAIL"), method = "resetRenderLists")
private void afterResetLists(final CallbackInfo ci) {
shipRenderLists.values().forEach(ChunkRenderList::clear);
shipRenderLists.clear();
}
}
Loading

0 comments on commit 9262503

Please sign in to comment.