Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix too many overlay breaking item and screen rendering (#1504 and #1553) #1555

Open
wants to merge 6 commits into
base: 1.21.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion patches/net/minecraft/client/gui/Gui.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
this.toolHighlightTimer = (int)(40.0 * this.minecraft.options.notificationDisplayTime().get());
} else if (this.toolHighlightTimer > 0) {
this.toolHighlightTimer--;
@@ -1292,8 +_,17 @@
@@ -1292,8 +_,25 @@
}
}

Expand All @@ -402,6 +402,14 @@
+ public int getLayerCount() {
+ return this.layerManager.getLayerCount();
+ }
+
+ /**
+ * Must call this at the beginning of overlays that could render ItemStack,
+ * to prevent visual bugs when rendering ItemStack at high z offset
+ */
+ public void clearDepth(GuiGraphics guiGraphics) {
+ this.layerManager.clearDepth(guiGraphics);
+ }
+
@OnlyIn(Dist.CLIENT)
- public static enum HeartType {
Expand Down
7 changes: 6 additions & 1 deletion patches/net/minecraft/client/gui/screens/Screen.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@
}

@Override
@@ -354,6 +_,7 @@
@@ -354,9 +_,12 @@

this.renderBlurredBackground(p_294317_);
this.renderMenuBackground(p_283688_);
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.client.event.ScreenEvent.BackgroundRendered(this, p_283688_));
}

protected void renderBlurredBackground(float p_330683_) {
+ // Neo: Fix screen not rendering when having too many overlays
+ RenderSystem.disableDepthTest();
this.minecraft.gameRenderer.processBlurEffect(p_330683_);
this.minecraft.getMainRenderTarget().bindWrite(false);
}
@@ -467,6 +_,10 @@
public void onFilesDrop(List<Path> p_96591_) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

package net.neoforged.neoforge.client.gui;

import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.VertexFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BooleanSupplier;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.LayeredDraw;
import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.fml.ModLoader;
import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent;
Expand All @@ -31,6 +35,7 @@ public class GuiLayerManager {
public static final float Z_SEPARATION = LayeredDraw.Z_SEPARATION;
private final List<NamedLayer> layers = new ArrayList<>();
private boolean initialized = false;
private int drawnLayerCount = -1;

public record NamedLayer(ResourceLocation name, LayeredDraw.Layer layer) {}

Expand All @@ -56,18 +61,36 @@ public void render(GuiGraphics guiGraphics, DeltaTracker partialTick) {
return;
}

drawnLayerCount = 0; // enable clearDepth
renderInner(guiGraphics, partialTick);

NeoForge.EVENT_BUS.post(new RenderGuiEvent.Post(guiGraphics, partialTick));
drawnLayerCount = -1; // disable clearDepth
}

private void renderInner(GuiGraphics guiGraphics, DeltaTracker partialTick) {
/**
* Reset z offset to prevent visual bugs caused by high z offset.
* Only effective when called inside {@link GuiLayerManager#render}
*/
public void clearDepth(GuiGraphics guiGraphics) {
// prevent unnecessary calls
if (drawnLayerCount <= 0) return;
drawnLayerCount = 0;
// clear depth values to keep hud rendered at the same depth
guiGraphics.pose().popPose();
guiGraphics.pose().pushPose();
guiGraphics.pose().translate(0, 0, -1000);
guiGraphics.fill(LayerRenderType.GUI, 0, 0, guiGraphics.guiWidth(), guiGraphics.guiHeight(), -1);
guiGraphics.pose().translate(0, 0, 1000);
}

private void renderInner(GuiGraphics guiGraphics, DeltaTracker partialTick) {
guiGraphics.pose().pushPose();
for (var layer : this.layers) {
if (!NeoForge.EVENT_BUS.post(new RenderGuiLayerEvent.Pre(guiGraphics, partialTick, layer.name(), layer.layer())).isCanceled()) {
layer.layer().render(guiGraphics, partialTick);
NeoForge.EVENT_BUS.post(new RenderGuiLayerEvent.Post(guiGraphics, partialTick, layer.name(), layer.layer()));
drawnLayerCount++;
}

guiGraphics.pose().translate(0.0F, 0.0F, Z_SEPARATION);
Expand All @@ -87,4 +110,21 @@ public void initModdedLayers() {
public int getLayerCount() {
return this.layers.size();
}

private static class LayerRenderType extends RenderType {
public static final RenderType GUI = create(
"reverse_gui",
DefaultVertexFormat.POSITION_COLOR,
VertexFormat.Mode.QUADS,
786432,
RenderType.CompositeState.builder()
.setShaderState(RENDERTYPE_GUI_SHADER)
.setWriteMaskState(RenderStateShard.DEPTH_WRITE)
.setDepthTestState(GREATER_DEPTH_TEST)
.createCompositeState(false));

public LayerRenderType(String name, VertexFormat format, VertexFormat.Mode mode, int bufferSize, boolean affectsCrumbling, boolean sortOnUpload, Runnable setupState, Runnable clearState) {
super(name, format, mode, bufferSize, affectsCrumbling, sortOnUpload, setupState, clearState);
}
}
}