Skip to content

Commit

Permalink
Add Early-Z optimization option (Fast Leaves)
Browse files Browse the repository at this point in the history
  • Loading branch information
thr3343 committed Aug 17, 2024
1 parent 2cc8ccf commit b229ef6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/vulkanmod/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Config {
public int advCulling = 2;
public boolean indirectDraw = false;

public boolean uniqueOpaqueLayer = true;
public boolean earlyZ = false;
public boolean entityCulling = true;
public int device = -1;

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/net/vulkanmod/config/option/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.platform.Window;
import net.minecraft.client.*;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.network.chat.Component;
import net.vulkanmod.Initializer;
import net.vulkanmod.config.Config;
Expand Down Expand Up @@ -249,13 +250,14 @@ public static OptionBlock[] getOptimizationOpts() {
value -> config.entityCulling = value,
() -> config.entityCulling)
.setTooltip(Component.translatable("vulkanmod.options.entityCulling.tooltip")),
new SwitchOption(Component.translatable("vulkanmod.options.uniqueOpaqueLayer"),
new SwitchOption(Component.translatable("vulkanmod.options.earlyZ"),
value -> {
config.uniqueOpaqueLayer = value;
config.earlyZ = value;
// ItemBlockRenderTypes.setFancy(!value);
minecraft.levelRenderer.allChanged();
},
() -> config.uniqueOpaqueLayer)
.setTooltip(Component.translatable("vulkanmod.options.uniqueOpaqueLayer.tooltip")),
() -> config.earlyZ)
.setTooltip(Component.translatable("vulkanmod.options.earlyZ.tooltip")),
new SwitchOption(Component.translatable("vulkanmod.options.indirectDraw"),
value -> config.indirectDraw = value,
() -> config.indirectDraw)
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/vulkanmod/render/PipelineManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.renderer.RenderType;
import net.vulkanmod.Initializer;
import net.vulkanmod.render.chunk.build.thread.ThreadBuilderPack;
import net.vulkanmod.render.vertex.CustomVertexFormat;
import net.vulkanmod.render.vertex.TerrainRenderType;
Expand Down Expand Up @@ -33,7 +34,11 @@ public static void init() {
}

public static void setDefaultShader() {
setShaderGetter(renderType -> renderType == TerrainRenderType.TRANSLUCENT ? terrainShaderEarlyZ : terrainShader);
setShaderGetter(renderType -> switch (renderType) {
case SOLID, TRANSLUCENT, TRIPWIRE -> terrainShaderEarlyZ;
case CUTOUT_MIPPED -> Initializer.CONFIG.earlyZ ? terrainShaderEarlyZ : terrainShader;
case CUTOUT -> terrainShader;
});
}

private static void createBasicPipelines() {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/net/vulkanmod/render/chunk/WorldRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@

import java.util.*;

import static org.lwjgl.opengl.GL11C.GL_LEQUAL;
import static org.lwjgl.opengl.GL11C.GL_LESS;

public class WorldRenderer {
private static WorldRenderer INSTANCE;

Expand Down Expand Up @@ -307,11 +310,12 @@ public void renderSectionLayer(RenderType renderType, PoseStack poseStack, doubl
this.minecraft.getProfiler().popPush(() -> "render_" + renderType);

final boolean isTranslucent = terrainRenderType == TerrainRenderType.TRANSLUCENT;
final boolean earlyZ = Initializer.CONFIG.earlyZ;
final boolean indirectDraw = Initializer.CONFIG.indirectDraw;

VRenderSystem.applyMVP(poseStack.last().pose(), projection);
VRenderSystem.setPrimitiveTopologyGL(GL11.GL_TRIANGLES);

VRenderSystem.depthFunc(earlyZ ? GL_LESS : GL_LEQUAL);
Renderer renderer = Renderer.getInstance();
GraphicsPipeline pipeline = PipelineManager.getTerrainShader(terrainRenderType);
renderer.bindGraphicsPipeline(pipeline);
Expand All @@ -322,8 +326,9 @@ public void renderSectionLayer(RenderType renderType, PoseStack poseStack, doubl
Renderer.getDrawer().bindIndexBuffer(Renderer.getCommandBuffer(), indexBuffer);

int currentFrame = Renderer.getCurrentFrame();
Set<TerrainRenderType> allowedRenderTypes = Initializer.CONFIG.uniqueOpaqueLayer ? TerrainRenderType.COMPACT_RENDER_TYPES : TerrainRenderType.SEMI_COMPACT_RENDER_TYPES;
Set<TerrainRenderType> allowedRenderTypes = Initializer.CONFIG.earlyZ ? TerrainRenderType.SEMI_COMPACT_RENDER_TYPES : TerrainRenderType.COMPACT_RENDER_TYPES;
if (allowedRenderTypes.contains(terrainRenderType)) {

terrainRenderType.setCutoutUniform();

for (Iterator<ChunkArea> iterator = this.sectionGraph.getChunkAreaQueue().iterator(isTranslucent); iterator.hasNext(); ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ private TerrainBufferBuilder getBufferBuilder(ThreadBuilderPack bufferBuilders,
}

private TerrainRenderType compactRenderTypes(TerrainRenderType renderType) {
if (Initializer.CONFIG.uniqueOpaqueLayer) {
if (Initializer.CONFIG.earlyZ) {
renderType = switch (renderType) {
case SOLID, CUTOUT, CUTOUT_MIPPED -> TerrainRenderType.CUTOUT_MIPPED;
case SOLID, CUTOUT_MIPPED -> TerrainRenderType.CUTOUT_MIPPED;
case CUTOUT -> TerrainRenderType.CUTOUT;
case TRANSLUCENT, TRIPWIRE -> TerrainRenderType.TRANSLUCENT;
};
} else {
renderType = switch (renderType) {
case SOLID, CUTOUT_MIPPED -> TerrainRenderType.CUTOUT_MIPPED;
case CUTOUT -> TerrainRenderType.CUTOUT;
case SOLID, CUTOUT, CUTOUT_MIPPED -> TerrainRenderType.CUTOUT_MIPPED;
case TRANSLUCENT, TRIPWIRE -> TerrainRenderType.TRANSLUCENT;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ private static int glToVulkan(int value) {
case 516 -> VK_COMPARE_OP_GREATER;
case 518 -> VK_COMPARE_OP_GREATER_OR_EQUAL;
case 514 -> VK_COMPARE_OP_EQUAL;
case 513 -> VK_COMPARE_OP_LESS;
default -> throw new RuntimeException("unknown blend factor..");

// case 515 -> VK_COMPARE_OP_GREATER_OR_EQUAL;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/assets/vulkanmod/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

"vulkanmod.options.refreshRate": "Refresh Rate",

"vulkanmod.options.uniqueOpaqueLayer": "Unique opaque layer",
"vulkanmod.options.uniqueOpaqueLayer.tooltip": "Use a unique render layer for opaque terrain to improve performance.",
"vulkanmod.options.earlyZ": "Early-Z (Fast Leaves)",
"vulkanmod.options.earlyZ.tooltip": "Enables Early-Z culling: (Experimental version of Vanilla's Fast leaves)\nHas the drawback of increased drawcalls, may be slower on some hardware",

"vulkanmod.options.windowedFullscreen": "Windowed Fullscreen"
}

0 comments on commit b229ef6

Please sign in to comment.