Skip to content

Commit

Permalink
update OcclusionCullerThread
Browse files Browse the repository at this point in the history
  • Loading branch information
RogoShum committed Apr 24, 2024
1 parent ad13539 commit 30d6bab
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 74 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.15.9

# Mod Properties
mod_version=0.5.1
mod_version=0.5.4
maven_group=rogo.renderingculling
archives_base_name=BruteForceRenderingCulling-fabric-1.20.1

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/rogo/renderingculling/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,25 @@ public static boolean getAsyncChunkRebuild() {
if(!shouldCullChunk())
return false;

if(CullingHandler.hasNvidium())
return false;

if(!CullingHandler.hasSodium())
return false;

return ASYNC.getValue();
}

public static void setAsyncChunkRebuild(boolean value) {
if(!shouldCullChunk())
return;

if(CullingHandler.hasNvidium())
return;

if(!CullingHandler.hasSodium())
return;

ASYNC.setValue(value);
save();
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/rogo/renderingculling/api/CullingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,7 @@ public static boolean shouldSkipEntity(Entity entity) {
public static void onProfilerPopPush(String s) {
switch (s) {
case "beforeRunTick" -> {
if (((AccessorLevelRender) Minecraft.getInstance().levelRenderer).getNeedsFullRenderChunkUpdate() && Minecraft.getInstance().level != null) {
fullChunkUpdateCooldown = 20;

if (Minecraft.getInstance().level != null) {
LEVEL_SECTION_RANGE = Minecraft.getInstance().level.getMaxSection() - Minecraft.getInstance().level.getMinSection();
LEVEL_MIN_SECTION_ABS = Math.abs(Minecraft.getInstance().level.getMinSection());
LEVEL_MIN_POS = Minecraft.getInstance().level.getMinBuildHeight();
Expand Down Expand Up @@ -710,9 +708,12 @@ public static boolean hasIris() {
return FabricLoader.getInstance().getAllMods().stream().anyMatch(modInfo -> modInfo.getMetadata().getId().equals("iris") || modInfo.getMetadata().getId().equals("oculus"));
}

public static boolean hasNvidium() {
return FabricLoader.getInstance().getAllMods().stream().anyMatch(modInfo -> modInfo.getMetadata().getId().equals("nvidium"));
}

public static boolean needPauseRebuild() {
//fullChunkUpdateCooldown > 0
return false;
return fullChunkUpdateCooldown > 0;
}

public static int mapChunkY(double posY) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rogo/renderingculling/api/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void onStartClientTick(Minecraft client) {
if (Minecraft.getInstance().player != null && Minecraft.getInstance().level != null) {
Config.loadConfig();
clientTickCount++;
if (Minecraft.getInstance().player.tickCount > 200 && clientTickCount > 200 && CHUNK_CULLING_MAP != null && !CHUNK_CULLING_MAP.isDone()) {
if (Minecraft.getInstance().player.tickCount > 60 && clientTickCount > 60 && CHUNK_CULLING_MAP != null && !CHUNK_CULLING_MAP.isDone()) {
CHUNK_CULLING_MAP.setDone();
LEVEL_SECTION_RANGE = Minecraft.getInstance().level.getMaxSection() - Minecraft.getInstance().level.getMinSection();
LEVEL_MIN_SECTION_ABS = Math.abs(Minecraft.getInstance().level.getMinSection());
Expand Down Expand Up @@ -122,6 +122,6 @@ public static int getB() {
}

public static Vector4f[] getFrustumPlanes(FrustumIntersection frustum) {
return ((AccessorFrustum.AccessorFrustumIntersection) ((AccessorFrustum) frustum).frustumIntersection()).planes();
return ((AccessorFrustum.AccessorFrustumIntersection) frustum).planes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface IRenderChunkInfo {
ChunkRenderDispatcher.RenderChunk getRenderChunk();

int getStep();
}
20 changes: 14 additions & 6 deletions src/main/java/rogo/renderingculling/gui/ConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
Expand Down Expand Up @@ -127,11 +128,11 @@ protected void init() {
}

//if (player.getName().getString().equals("Dev")) {
addConfigButton(() -> CullingHandler.checkCulling, (b) -> CullingHandler.checkCulling = b, () -> Component.literal("Debug"))
.setDetailMessage(() -> Component.translatable("brute_force_rendering_culling.detail.debug"));
addConfigButton(() -> CullingHandler.checkCulling, (b) -> CullingHandler.checkCulling = b, () -> Component.literal("Debug"))
.setDetailMessage(() -> Component.translatable("brute_force_rendering_culling.detail.debug"));

addConfigButton(() -> CullingHandler.checkTexture, (b) -> CullingHandler.checkTexture = b, () -> Component.literal("Check Texture"))
.setDetailMessage(() -> Component.translatable("brute_force_rendering_culling.detail.check_texture"));
addConfigButton(() -> CullingHandler.checkTexture, (b) -> CullingHandler.checkTexture = b, () -> Component.literal("Check Texture"))
.setDetailMessage(() -> Component.translatable("brute_force_rendering_culling.detail.check_texture"));
//}

addConfigButton(Config::getSampling, (value) -> {
Expand All @@ -156,8 +157,15 @@ protected void init() {
}, () -> Component.translatable("brute_force_rendering_culling.culling_map_update_delay"))
.setDetailMessage(() -> Component.translatable("brute_force_rendering_culling.detail.culling_map_update_delay"));

addConfigButton(Config::getCullChunk, Config::getAsyncChunkRebuild, Config::setAsyncChunkRebuild, () -> Component.translatable("brute_force_rendering_culling.async"))
.setDetailMessage(() -> Component.translatable("brute_force_rendering_culling.detail.async"));
addConfigButton(() -> Config.getCullChunk() && CullingHandler.hasSodium() && !CullingHandler.hasNvidium(), Config::getAsyncChunkRebuild, Config::setAsyncChunkRebuild, () -> Component.translatable("brute_force_rendering_culling.async"))
.setDetailMessage(() -> {
if (CullingHandler.hasNvidium()) {
return Component.translatable("brute_force_rendering_culling.detail.nvidium");
} else if (!CullingHandler.hasSodium()) {
return Component.translatable("brute_force_rendering_culling.detail.sodium");
} else
return Component.translatable("brute_force_rendering_culling.detail.async");
});
addConfigButton(Config::getCullChunk, Config::setCullChunk, () -> Component.translatable("brute_force_rendering_culling.cull_chunk"))
.setDetailMessage(() -> Component.translatable("brute_force_rendering_culling.detail.cull_chunk"));
addConfigButton(Config::getCullEntity, Config::setCullEntity, () -> Component.translatable("brute_force_rendering_culling.cull_entity"))
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/rogo/renderingculling/gui/NeatButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
Expand Down Expand Up @@ -91,7 +92,12 @@ public void shouDetail(GuiGraphics guiGraphics, Font font) {
List<Component> components = new ArrayList<>();
String[] parts = detailMessage.get().getString().split("\\n");
for (String part : parts) {
components.add(Component.literal(part));
Component text = Component.literal(part);
if(part.contains("warn:")) {
String[] warn = part.split("warn:");
text = Component.literal(warn[1]).withStyle(ChatFormatting.DARK_RED);
}
components.add(text);
}
guiGraphics.renderComponentTooltip(font, components, this.getX() + this.width / 2, this.getY() + (this.height - 8) / 2);
CullingHandler.reColorToolTip = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
Expand Down Expand Up @@ -88,7 +89,12 @@ public void shouDetail(GuiGraphics guiGraphics, Font font) {
List<Component> components = new ArrayList<>();
String[] parts = detailMessage.get().getString().split("\\n");
for (String part : parts) {
components.add(Component.literal(part));
Component text = Component.literal(part);
if(part.contains("warn:")) {
String[] warn = part.split("warn:");
text = Component.literal(warn[1]).withStyle(ChatFormatting.DARK_RED);
}
components.add(text);
}
guiGraphics.renderComponentTooltip(font, components, this.getX() + this.width / 2, this.getY() + (this.height - 8) / 2);
CullingHandler.reColorToolTip = false;
Expand Down
60 changes: 25 additions & 35 deletions src/main/java/rogo/renderingculling/mixin/MixinLevelRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -28,26 +28,32 @@

import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.LinkedHashSet;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicReference;

@Mixin(LevelRenderer.class)
public abstract class MixinLevelRender implements IEntitiesForRender {

@Mutable
@Final
@Shadow
private ObjectArrayList<?> renderChunksInFrustum;
private ObjectArrayList<LevelRenderer.RenderChunkInfo> renderChunksInFrustum;

@Shadow @Nullable protected abstract ChunkRenderDispatcher.RenderChunk getRelativeFrom(BlockPos p_109729_, ChunkRenderDispatcher.RenderChunk p_109730_, Direction p_109731_);
@Shadow
@Nullable
protected abstract ChunkRenderDispatcher.RenderChunk getRelativeFrom(BlockPos p_109729_, ChunkRenderDispatcher.RenderChunk p_109730_, Direction p_109731_);

@Shadow @Nullable private ViewArea viewArea;
@Shadow
@Nullable
private ViewArea viewArea;

@Shadow @Final private AtomicReference<LevelRenderer.RenderChunkStorage> renderChunkStorage;
@Shadow
@Final
private AtomicReference<LevelRenderer.RenderChunkStorage> renderChunkStorage;
private LevelRenderer.RenderChunkStorage renderChunkStorageTemp;

@Inject(method = "applyFrustum", at = @At(value = "RETURN"))
public void onApplyFrustum(Frustum p_194355_, CallbackInfo ci) {
if (Config.shouldCullChunk() && !Config.getAsyncChunkRebuild()) {
if (Config.shouldCullChunk() && !VanillaAsyncUtil.shouldReplaceStorage()) {
if (CullingHandler.OptiFine != null) {
try {
Field field = LevelRenderer.class.getDeclaredField("renderInfosTerrain");
Expand All @@ -71,31 +77,25 @@ public void onApplyFrustum(Frustum p_194355_, CallbackInfo ci) {
}
}

@Inject(method = "updateRenderChunks", at = @At(value = "INVOKE", target = "Ljava/util/Queue;isEmpty()Z"), cancellable = true)
public void onUpdateRenderChunks(LinkedHashSet<LevelRenderer.RenderChunkInfo> p_194363_, LevelRenderer.RenderInfoMap p_194364_, Vec3 p_194365_, Queue<LevelRenderer.RenderChunkInfo> p_194366_, boolean p_194367_, CallbackInfo ci) {
if (Config.getAsyncChunkRebuild()) {
ci.cancel();
}
}

@Inject(method = "setupRender", at = @At(value = "HEAD"))
public void onSetupRenderHead(Camera p_194339_, Frustum p_194340_, boolean p_194341_, boolean p_194342_, CallbackInfo ci) {
if(this.viewArea != null) {
VanillaAsyncUtil.update((LevelRenderer) (Object)this, this.viewArea.chunks.length);
if (this.viewArea != null) {
VanillaAsyncUtil.update((LevelRenderer) (Object) this, this.viewArea.chunks.length);
}
}

@Inject(method = "setupRender", at = @At(value = "RETURN"))
public void onSetupRenderEnd(Camera p_194339_, Frustum p_194340_, boolean p_194341_, boolean p_194342_, CallbackInfo ci) {
if (Config.getAsyncChunkRebuild() && VanillaAsyncUtil.getChunkStorage() != null) {
@Inject(method = "applyFrustum", at = @At(value = "HEAD"))
public void onApplyFrustumHead(Frustum p_194355_, CallbackInfo ci) {
if (VanillaAsyncUtil.shouldReplaceStorage()) {
renderChunkStorageTemp = this.renderChunkStorage.get();
this.renderChunkStorage.set(VanillaAsyncUtil.getChunkStorage());
}
}

@Inject(method = "initializeQueueForFullUpdate", at = @At(value = "HEAD"), cancellable = true)
public void onInitializeQueueForFullUpdate(Camera p_194344_, Queue<LevelRenderer.RenderChunkInfo> p_194345_, CallbackInfo ci) {
if (Config.getAsyncChunkRebuild()) {
ci.cancel();
@Inject(method = "applyFrustum", at = @At(value = "RETURN"))
public void onApplyFrustumReturn(Frustum p_194355_, CallbackInfo ci) {
if (VanillaAsyncUtil.shouldReplaceStorage()) {
this.renderChunkStorage.set(renderChunkStorageTemp);
}
}

Expand All @@ -116,17 +116,7 @@ public ChunkRenderDispatcher.RenderChunk invokeGetRelativeFrom(BlockPos pos, Chu

@Override
public ChunkRenderDispatcher.RenderChunk invokeGetRenderChunkAt(BlockPos pos) {
return ((AccessorViewArea)this.viewArea).invokeGetRenderChunkAt(pos);
}

@Mixin(LevelRenderer.RenderChunkInfo.class)
public interface AccessorRenderChunkInfo {

@Accessor("directions")
byte getDirections();

@Accessor("step")
int getStep();
return ((AccessorViewArea) this.viewArea).invokeGetRenderChunkAt(pos);
}

@Mixin(ViewArea.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ public class MixinRenderChunkInfo implements IRenderChunkInfo {
public ChunkRenderDispatcher.RenderChunk getRenderChunk() {
return chunk;
}

@Shadow
@Final
int step;

@Override
public int getStep() {
return this.step;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rogo.renderingculling.mixin.sodium;

import com.mojang.blaze3d.systems.RenderSystem;
import me.jellysquid.mods.sodium.client.render.chunk.RenderSection;
import me.jellysquid.mods.sodium.client.render.chunk.occlusion.OcclusionCuller;
import me.jellysquid.mods.sodium.client.render.viewport.Viewport;
Expand All @@ -25,8 +26,7 @@ private static void onIsSectionVisible(RenderSection section, Viewport viewport,

@Inject(method = "findVisible", at = @At(value = "HEAD"), remap = false, cancellable = true)
private void onFindVisible(OcclusionCuller.Visitor visitor, Viewport viewport, float searchDistance, boolean useOcclusionCulling, int frame, CallbackInfo ci) {
if (SodiumSectionAsyncUtil.asyncSearching && Thread.currentThread() != OcclusionCullerThread.INSTANCE) {
SodiumSectionAsyncUtil.asyncSearching = false;
if (Config.getAsyncChunkRebuild() && RenderSystem.isOnRenderThread()) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ private void onIsSectionVisible(int x, int y, int z, CallbackInfoReturnable<Bool
@ModifyVariable(name = "visitor", method = "createTerrainRenderList", at = @At(value = "INVOKE", target = "Lme/jellysquid/mods/sodium/client/render/chunk/occlusion/OcclusionCuller;findVisible(Lme/jellysquid/mods/sodium/client/render/chunk/occlusion/OcclusionCuller$Visitor;Lme/jellysquid/mods/sodium/client/render/viewport/Viewport;FZI)V", shift = At.Shift.BEFORE), remap = false)
private VisibleChunkCollector onCreateTerrainRenderList(VisibleChunkCollector value) {
if (Config.getAsyncChunkRebuild() && !CullingHandler.needPauseRebuild()) {
SodiumSectionAsyncUtil.asyncSearching = true;
VisibleChunkCollector collector = CullingHandler.renderingIris() ? SodiumSectionAsyncUtil.getShadowCollector() : SodiumSectionAsyncUtil.getChunkCollector();
return collector == null ? value : collector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ public void run() {
try {
if (CullingHandler.CHUNK_CULLING_MAP != null && CullingHandler.CHUNK_CULLING_MAP.isDone()) {
if (Config.getAsyncChunkRebuild()) {
//CullingHandler.CHUNK_CULLING_MAP.updateVisibleChunks();
if (CullingHandler.hasSodium()) {
SodiumSectionAsyncUtil.asyncSearchRebuildSection();
} else {
//VanillaAsyncUtil.asyncSearchRebuildSection();
VanillaAsyncUtil.asyncSearchRebuildSection();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ public class SodiumSectionAsyncUtil {
private static Viewport shadowViewport;
private static float shadowSearchDistance;
private static boolean shadowUseOcclusionCulling;
private static volatile boolean shouldSearch = true;
private static VisibleChunkCollector collector;
private static VisibleChunkCollector shadowCollector;
public static boolean renderingEntities;
public static boolean asyncSearching;

public static void fromSectionManager(Long2ReferenceMap<RenderSection> sections, Level world) {
SodiumSectionAsyncUtil.occlusionCuller = new OcclusionCuller(sections, world);
Expand Down
Loading

0 comments on commit 30d6bab

Please sign in to comment.