Skip to content

Commit

Permalink
update Async rebuild chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
RogoShum committed Apr 26, 2024
1 parent 0104a4e commit ca3aebe
Show file tree
Hide file tree
Showing 22 changed files with 271 additions and 248 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.4
mod_version=0.5.5
maven_group=rogo.renderingculling
archives_base_name=BruteForceRenderingCulling-fabric-1.20.1

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/rogo/renderingculling/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Config {

public static double getSampling() {
if(unload())
return 0.2;
return 0.5;

return SAMPLING.getValue();
}
Expand Down Expand Up @@ -75,10 +75,10 @@ public static boolean getAsyncChunkRebuild() {
if(!shouldCullChunk())
return false;

if(CullingHandler.hasNvidium())
if(ModLoader.hasNvidium())
return false;

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

return ASYNC.getValue();
Expand All @@ -88,10 +88,10 @@ public static void setAsyncChunkRebuild(boolean value) {
if(!shouldCullChunk())
return;

if(CullingHandler.hasNvidium())
if(ModLoader.hasNvidium())
return;

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

ASYNC.setValue(value);
Expand Down
95 changes: 49 additions & 46 deletions src/main/java/rogo/renderingculling/api/CullingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.pipeline.TextureTarget;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.logging.LogUtils;
import com.mojang.math.Axis;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Camera;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.client.renderer.culling.Frustum;
Expand All @@ -25,7 +21,6 @@
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.joml.Matrix4f;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.Checks;
import org.slf4j.Logger;
Expand All @@ -38,7 +33,7 @@
import rogo.renderingculling.mixin.AccessorMinecraft;
import rogo.renderingculling.util.DepthContext;
import rogo.renderingculling.util.LifeTimer;
import rogo.renderingculling.util.NvidiumUtil;
import rogo.renderingculling.util.OcclusionCullerThread;
import rogo.renderingculling.util.ShaderLoader;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -137,22 +132,6 @@ public class CullingHandler {
});
}

public static String fromID(String s) {
return MOD_ID + ":" + s;
}

public static final KeyMapping CONFIG_KEY = KeyBindingHelper.registerKeyBinding(
new KeyMapping(MOD_ID + ".key.config",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_R,
"key.category." + MOD_ID));

public static final KeyMapping DEBUG_KEY = KeyBindingHelper.registerKeyBinding(
new KeyMapping(MOD_ID + ".key.debug",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_X,
"key.category." + MOD_ID));

public static void init() {
try {
OptiFine = Class.forName("net.optifine.shaders.Shaders");
Expand All @@ -167,7 +146,7 @@ public static void init() {
}
}

if (hasIris()) {
if (ModLoader.hasIris()) {
try {
SHADER_LOADER = Class.forName("rogo.renderingculling.util.IrisLoaderImpl").asSubclass(ShaderLoader.class).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
Expand Down Expand Up @@ -203,6 +182,19 @@ public static boolean shouldRenderChunk(IRenderSectionVisibility section, boolea
return false;
}

if (DEBUG < 2) {
if (!useOcclusionCulling) {
return true;
}
if (!section.shouldCheckVisibility(lastVisibleUpdatedFrame)) {
return true;
} else if (CHUNK_CULLING_MAP.isChunkOffsetCameraVisible(section.getPositionX(), section.getPositionY(), section.getPositionZ())) {
section.updateVisibleTick(lastVisibleUpdatedFrame);
return true;
}
return false;
}

if (Config.getAsyncChunkRebuild()) {
if (!useOcclusionCulling) {
return true;
Expand Down Expand Up @@ -251,11 +243,20 @@ public static boolean shouldSkipBlockEntity(BlockEntity blockEntity, AABB aabb,
}

if (ENTITY_CULLING_MAP == null || !Config.getCullEntity()) return false;
if (FRUSTUM == null || !FRUSTUM.isVisible(aabb)) return true;
String type = BlockEntityType.getKey(blockEntity.getType()).toString();
if (Config.getBlockEntitiesSkip().contains(type))
return false;

if (DEBUG < 2) {
if (visibleBlock.contains(pos)) {
return false;
} else if (ENTITY_CULLING_MAP.isObjectVisible(blockEntity)) {
visibleBlock.updateUsageTick(pos, clientTickCount);
return false;
}
return true;
}

long time = System.nanoTime();

boolean visible;
Expand Down Expand Up @@ -289,6 +290,16 @@ public static boolean shouldSkipEntity(Entity entity) {
return false;
if (ENTITY_CULLING_MAP == null || !Config.getCullEntity()) return false;

if (DEBUG < 2) {
if (visibleEntity.contains(entity)) {
return false;
} else if (ENTITY_CULLING_MAP.isObjectVisible(entity)) {
visibleEntity.updateUsageTick(entity, clientTickCount);
return false;
}
return true;
}

long time = System.nanoTime();

boolean visible;
Expand Down Expand Up @@ -317,7 +328,11 @@ public static boolean shouldSkipEntity(Entity entity) {
public static void onProfilerPopPush(String s) {
switch (s) {
case "beforeRunTick" -> {
if (Minecraft.getInstance().level != null) {
if (((AccessorLevelRender) Minecraft.getInstance().levelRenderer).getNeedsFullRenderChunkUpdate() && Minecraft.getInstance().level != null) {
if(ModLoader.hasMod("embeddium")) {
fullChunkUpdateCooldown = 20;
}

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 All @@ -327,6 +342,7 @@ public static void onProfilerPopPush(String s) {
case "afterRunTick" -> {
++frame;
updateMapData();
OcclusionCullerThread.shouldUpdate();
}
case "captureFrustum" -> {
AccessorLevelRender levelFrustum = (AccessorLevelRender) Minecraft.getInstance().levelRenderer;
Expand Down Expand Up @@ -363,9 +379,10 @@ public static void onProfilerPopPush(String s) {
}

public static void onProfilerPush(String s) {
if(s.equals("onKeyboardInput")) {
ModLoader.onKeyPress();
} if (Config.shouldCullChunk() && s.equals("apply_frustum")) {
if (s.equals("onKeyboardInput")) {
ModLoader.onKeyPress();
}
if (Config.shouldCullChunk() && s.equals("apply_frustum")) {
if (SHADER_LOADER == null || OptiFine != null) {
chunkCount = 0;
chunkCulling = 0;
Expand Down Expand Up @@ -481,7 +498,7 @@ public static void checkShader() {
}

public static void updateDepthMap() {
CullingHandler.PROJECTION_MATRIX = new Matrix4f(RenderSystem.getProjectionMatrix());
//CullingHandler.PROJECTION_MATRIX = new Matrix4f(RenderSystem.getProjectionMatrix());
if (anyCulling() && !checkCulling && anyNeedTransfer()) {
float sampling = (float) (double) Config.getSampling();
Window window = Minecraft.getInstance().getWindow();
Expand Down Expand Up @@ -612,6 +629,8 @@ public static void updateMapData() {

CullingHandler.ENTITY_CULLING_MAP.getEntityTable().addAllTemp();
}

CullingHandler.ENTITY_CULLING_MAP.getEntityTable().addEntityAttribute(CullingRenderEvent.ENTITY_CULLING_INSTANCE_RENDERER::addInstanceAttrib);
}

fps = ((AccessorMinecraft) Minecraft.getInstance()).getFps();
Expand Down Expand Up @@ -697,24 +716,8 @@ public static boolean gl33() {
return gl33 == 1;
}

public static boolean hasMod(String s) {
return FabricLoader.getInstance().getAllMods().stream().anyMatch(modInfo -> modInfo.getMetadata().getId().equals(s));
}

public static boolean hasSodium() {
return FabricLoader.getInstance().getAllMods().stream().anyMatch(modInfo -> modInfo.getMetadata().getId().equals("sodium") || modInfo.getMetadata().getId().equals("embeddium") || modInfo.getMetadata().getId().equals("rubidium"));
}

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")) && NvidiumUtil.nvidiumBfs();
}

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

public static int mapChunkY(double posY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ public static void setUniform(ShaderInstance shader) {
float[] array = new float[]{pos.x, pos.y, pos.z};
shaderInstance.getCullingCameraDir().set(array);
}
if (shaderInstance.getCullingFov() != null) {
shaderInstance.getCullingFov().set((float) CullingHandler.FOV + (float) (170 - CullingHandler.FOV));
if (shaderInstance.getBoxScale() != null) {
shaderInstance.getBoxScale().set(4.0f);
}
if (shaderInstance.getFrustumPos() != null && CullingHandler.FRUSTUM != null) {
Vec3 pos = new Vec3(
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/rogo/renderingculling/api/ModLoader.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package rogo.renderingculling.api;

import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.Level;
import org.joml.FrustumIntersection;
import org.joml.Vector4f;
import org.lwjgl.glfw.GLFW;
import rogo.renderingculling.event.WorldUnloadEvent;
import rogo.renderingculling.gui.ConfigScreen;
import rogo.renderingculling.mixin.AccessorFrustum;
import rogo.renderingculling.util.NvidiumUtil;
import rogo.renderingculling.util.OcclusionCullerThread;

import java.io.IOException;
Expand Down Expand Up @@ -70,6 +75,10 @@ private void initShader() {
}
}

public static String fromID(String s) {
return MOD_ID + ":" + s;
}

private void registerEvents() {
WorldUnloadEvent.WORLD_UNLOAD.register(this::onWorldUnload);
ClientTickEvents.START_CLIENT_TICK.register(this::onStartClientTick);
Expand Down Expand Up @@ -102,6 +111,18 @@ private void onStartClientTick(Minecraft client) {
}
}

public static final KeyMapping CONFIG_KEY = KeyBindingHelper.registerKeyBinding(
new KeyMapping(MOD_ID + ".key.config",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_R,
"key.category." + MOD_ID));

public static final KeyMapping DEBUG_KEY = KeyBindingHelper.registerKeyBinding(
new KeyMapping(MOD_ID + ".key.debug",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_X,
"key.category." + MOD_ID));

public static void onKeyPress() {
if (CONFIG_KEY.isDown()) {
Minecraft.getInstance().setScreen(new ConfigScreen(Component.translatable(MOD_ID + ".config")));
Expand All @@ -124,4 +145,20 @@ public static int getB() {
public static Vector4f[] getFrustumPlanes(FrustumIntersection frustum) {
return ((AccessorFrustum.AccessorFrustumIntersection) frustum).planes();
}

public static boolean hasMod(String s) {
return FabricLoader.getInstance().getAllMods().stream().anyMatch(modInfo -> modInfo.getMetadata().getId().equals(s));
}

public static boolean hasSodium() {
return FabricLoader.getInstance().getAllMods().stream().anyMatch(modInfo -> modInfo.getMetadata().getId().equals("sodium") || modInfo.getMetadata().getId().equals("embeddium") || modInfo.getMetadata().getId().equals("rubidium"));
}

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")) && NvidiumUtil.nvidiumBfs();
}
}
Loading

0 comments on commit ca3aebe

Please sign in to comment.