Skip to content

Commit b820ef6

Browse files
committed
feat: Combined depth for RenderWorldLastEvent
1 parent df2b5f4 commit b820ef6

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/main/java/com/ventooth/swansong/mixin/mixins/client/hooks/EntityRendererMixin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,12 @@ private boolean renderWorld_anaglyph(GameSettings instance) {
132132
remap = false),
133133
require = 1)
134134
private void hook_RenderLastAndEndRenderWorld(RenderGlobal rg, float subTick, Operation<Void> original) {
135-
original.call(rg, subTick);
136135
if (ShaderEngine.isInitialized()) {
136+
ShaderEngine.preRenderLast();
137+
original.call(rg, subTick);
137138
ShaderEngine.finishRenderFinal();
139+
} else {
140+
original.call(rg, subTick);
138141
}
139142
}
140143

src/main/java/com/ventooth/swansong/shader/ShaderEngine.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import net.minecraft.tileentity.TileEntity;
6565
import net.minecraft.world.WorldProvider;
6666
import net.minecraftforge.client.ForgeHooksClient;
67+
import net.minecraftforge.client.event.RenderWorldLastEvent;
6768

6869
import java.nio.DoubleBuffer;
6970
import java.util.ArrayList;
@@ -783,9 +784,37 @@ public static void beginRenderWorld() {
783784
renderShadowMap();
784785
}
785786

787+
/**
788+
* Experimental feature that might either be just left in, or put behind a toggle depending on perf overhead.
789+
* <p>
790+
* What we're doing is handing whatever will render in the {@link RenderWorldLastEvent} depth that excludes translucent geometry.
791+
* <p>
792+
* This is closer to what vanilla does, but in turn this will cost us an additional 3 full-screen depth blits.
793+
*/
794+
private static final boolean COMBINED_DEPTH = true;
795+
796+
public static void preRenderLast() {
797+
if (COMBINED_DEPTH) {
798+
DebugMarker.GENERIC.insert("PRE_SAVE_DEPTH_0");
799+
blitDepth(buffers.gDepthTex, buffers.depthTex0);
800+
DebugMarker.GENERIC.insert("POST_SAVE_DEPTH_0");
801+
802+
DebugMarker.GENERIC.insert("PRE_LOAD_DEPTH_1");
803+
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
804+
blitDepth(buffers.depthTex1, buffers.gDepthTex);
805+
DebugMarker.GENERIC.insert("POST_LOAD_DEPTH_1");
806+
}
807+
}
808+
786809
public static void finishRenderFinal() {
787810
ShaderState.updateRenderStage(MCRenderStage.NONE);
788811

812+
if (COMBINED_DEPTH) {
813+
DebugMarker.GENERIC.insert("PRE_COMBINE_DEPTH");
814+
blitDepth(buffers.depthTex0, buffers.gDepthTex, true);
815+
DebugMarker.GENERIC.insert("POST_COMBINE_DEPTH");
816+
}
817+
789818
captureLastDepth();
790819
// Composite first
791820
renderComposite();
@@ -1156,6 +1185,10 @@ public static void blitColors(Int2ObjectMap<Texture2D> src, Int2ObjectMap<Textur
11561185
}
11571186

11581187
public static void blitDepth(Texture2D srcTex, Texture2D dstTex) {
1188+
blitDepth(srcTex, dstTex, false);
1189+
}
1190+
1191+
public static void blitDepth(Texture2D srcTex, Texture2D dstTex, boolean combine) {
11591192
if (state == null) {
11601193
Share.log.warn("Tried to blit depth with no state");
11611194
return;
@@ -1183,10 +1216,12 @@ public static void blitDepth(Texture2D srcTex, Texture2D dstTex) {
11831216
// Bind Destination Texture
11841217
dstTex.attachToFramebufferDepth();
11851218

1186-
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
1219+
if (!combine) {
1220+
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
1221+
}
11871222

11881223
// Draw using the regular composite (with depth only)
1189-
ShadersCompositeMesh.drawWithDepth();
1224+
ShadersCompositeMesh.drawWithDepth(combine);
11901225

11911226
if (DebugMarker.isEnabled()) {
11921227
DebugMarker.TEXTURE_DEPTH_BLIT.insertFormat("{0} -> {1}", srcTex.name(), dstTex.name());

src/main/java/com/ventooth/swansong/shader/ShadersCompositeMesh.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public static void drawWithAnaglyphField(int field) {
3838
}
3939
}
4040

41-
public static void drawWithDepth() {
42-
draw(Mask.Depth);
41+
public static void drawWithDepth(boolean combine) {
42+
draw(combine ? Mask.DepthCombine : Mask.Depth);
4343
}
4444

4545
public static void draw(Mask mask) {
@@ -60,10 +60,15 @@ public static void draw(Mask mask) {
6060
GL11.glEnable(GL11.GL_TEXTURE_2D);
6161
GL11.glDisable(GL11.GL_ALPHA_TEST);
6262
GL11.glDisable(GL11.GL_BLEND);
63-
GL11.glEnable(GL11.GL_DEPTH_TEST);
64-
GL11.glDepthFunc(GL11.GL_ALWAYS);
6563
GL11.glDisable(GL11.GL_LIGHTING);
6664

65+
GL11.glEnable(GL11.GL_DEPTH_TEST);
66+
if (mask == Mask.DepthCombine) {
67+
GL11.glDepthFunc(GL11.GL_LEQUAL);
68+
} else {
69+
GL11.glDepthFunc(GL11.GL_ALWAYS);
70+
}
71+
6772
switch (mask) {
6873
case Color -> {
6974
GL11.glDepthMask(false);
@@ -77,7 +82,7 @@ public static void draw(Mask mask) {
7782
GL11.glDepthMask(false);
7883
GL11.glColorMask(false, true, true, false);
7984
}
80-
case Depth -> {
85+
case Depth, DepthCombine -> {
8186
GL11.glDepthMask(true);
8287
GL11.glColorMask(false, false, false, false);
8388
}
@@ -178,6 +183,7 @@ public enum Mask {
178183
AnaglyphRed,
179184
AnaglyphCyan,
180185
Depth,
186+
DepthCombine,
181187
Both
182188
}
183189
}

0 commit comments

Comments
 (0)