Skip to content

Commit 65d9bf8

Browse files
Pistonightmitchej123
authored andcommitted
copy depth buffer from MC for HUD caching
1 parent 3ee82e7 commit 65d9bf8

4 files changed

Lines changed: 51 additions & 54 deletions

File tree

src/main/java/com/gtnewhorizons/angelica/hudcaching/HUDCaching.java

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import org.lwjgl.opengl.GL11;
7+
import org.lwjgl.opengl.GL30;
78

89
import com.gtnewhorizons.angelica.glsm.GLStateManager;
910
import com.gtnewhorizons.angelica.glsm.TessellatorManager;
@@ -34,10 +35,7 @@ public class HUDCaching {
3435
public static Framebuffer framebuffer;
3536
static {
3637
framebuffer = new Framebuffer(0, 0, true);
37-
framebuffer.framebufferColor[0] = 0.0F;
38-
framebuffer.framebufferColor[1] = 0.0F;
39-
framebuffer.framebufferColor[2] = 0.0F;
40-
framebuffer.framebufferColor[3] = 0.0F;
38+
framebuffer.setFramebufferColor(0, 0, 0, 0);
4139
}
4240
private static boolean dirty = true;
4341

@@ -112,30 +110,23 @@ public static void renderCachedHud(EntityRenderer renderer, GuiIngame ingame, fl
112110
return;
113111
}
114112

115-
GLStateManager.enableDepthTest();
116-
ScaledResolution resolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
117-
int width = resolution.getScaledWidth();
118-
int height = resolution.getScaledHeight();
119-
renderer.setupOverlayRendering();
120-
GLStateManager.enableBlend();
121-
122113
if (dirty) {
123114
dirty = false;
124-
checkFramebufferSizes(mc.displayWidth, mc.displayHeight);
125-
framebuffer.framebufferClear();
115+
resetFramebuffer(mc.displayWidth, mc.displayHeight);
126116
framebuffer.bindFramebuffer(false);
127-
GLStateManager.disableBlend();
128-
GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
129-
GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
130-
GLStateManager.disableLighting();
131-
GLStateManager.disableFog();
132117
renderingCacheOverride = true;
133118
ingame.renderGameOverlay(partialTicks, hasScreen, mouseX, mouseY);
134119
renderingCacheOverride = false;
135120
mc.getFramebuffer().bindFramebuffer(false);
136-
GLStateManager.enableBlend();
121+
} else {
122+
renderer.setupOverlayRendering();
137123
}
138124

125+
ScaledResolution resolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
126+
int width = resolution.getScaledWidth();
127+
int height = resolution.getScaledHeight();
128+
GLStateManager.enableBlend();
129+
139130
// reset the color that may be applied by some items
140131
GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
141132

@@ -148,8 +139,6 @@ public static void renderCachedHud(EntityRenderer renderer, GuiIngame ingame, fl
148139
GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
149140
}
150141

151-
Tessellator tessellator = TessellatorManager.get();
152-
153142
if (ingame instanceof GuiIngameForge) {
154143
GuiIngameForgeAccessor guiForge = ((GuiIngameForgeAccessor) ingame);
155144
if (renderHelmetCaptured) {
@@ -168,36 +157,46 @@ public static void renderCachedHud(EntityRenderer renderer, GuiIngame ingame, fl
168157
}
169158
if (renderPortalCapturedTicks > 0)
170159
{
171-
gui.callFunc_130015_b(renderPortalCapturedTicks, width, height);
172-
}
173-
if (renderCrosshairsCaptured) {
174-
mc.getTextureManager().bindTexture(Gui.icons);
175-
GLStateManager.enableBlend();
176-
GLStateManager.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, GL11.GL_ONE, GL11.GL_ZERO);
177-
GLStateManager.enableAlphaTest();
178-
drawTexturedModalRect(tessellator, (width >> 1) - 7, (height >> 1) - 7);
179-
GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
160+
gui.callRenderPortal(renderPortalCapturedTicks, width, height);
180161
}
181162
}
182163

183164
// render cached frame
165+
Tessellator tessellator = TessellatorManager.get();
184166
GLStateManager.enableBlend();
185167
GLStateManager.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
186168
GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
187169
framebuffer.bindFramebufferTexture();
188170
drawTexturedRect(tessellator, (float) resolution.getScaledWidth_double(), (float) resolution.getScaledHeight_double());
189-
GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
190-
191-
GLStateManager.enableDepthTest();
171+
172+
GLStateManager.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
173+
mc.getTextureManager().bindTexture(Gui.icons);
174+
}
175+
176+
/**
177+
* We are skipping certain render calls when rendering into cache,
178+
* however, we cannot skip the GL state changes. This will fix
179+
* the state before we start rendering
180+
*/
181+
public static void fixGLStateBeforeRenderingCache() {
182+
GLStateManager.glDepthMask(true);
183+
GLStateManager.enableDepthTest();
184+
GLStateManager.enableAlphaTest();
185+
GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
186+
GLStateManager.disableBlend();
192187
}
193188

194-
private static void checkFramebufferSizes(int width, int height) {
189+
private static void resetFramebuffer(int width, int height) {
195190
if (framebuffer.framebufferWidth != width || framebuffer.framebufferHeight != height) {
196-
framebuffer.createBindFramebuffer(width, height);
197-
framebuffer.framebufferWidth = width;
198-
framebuffer.framebufferHeight = height;
191+
framebuffer.createBindFramebuffer(width, height);
199192
framebuffer.setFramebufferFilter(GL11.GL_NEAREST);
193+
} else {
194+
framebuffer.framebufferClear();
200195
}
196+
// copy depth buffer from MC
197+
OpenGlHelper.func_153171_g(GL30.GL_READ_FRAMEBUFFER, mc.getFramebuffer().framebufferObject);
198+
OpenGlHelper.func_153171_g(GL30.GL_DRAW_FRAMEBUFFER, framebuffer.framebufferObject);
199+
GL30.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT, GL11.GL_NEAREST);
201200
}
202201

203202
private static void drawTexturedRect(Tessellator tessellator, float width, float height) {
@@ -214,13 +213,4 @@ private static void drawTexturedRect(Tessellator tessellator, float width, float
214213
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
215214
}
216215

217-
private static void drawTexturedModalRect(Tessellator tessellator, int x, int y) {
218-
tessellator.startDrawingQuads();
219-
tessellator.addVertexWithUV(x, y + 16, 100.0, 0.0, 0.0625);
220-
tessellator.addVertexWithUV(x + 16, y + 16, 100.0, 0.0625, 0.0625);
221-
tessellator.addVertexWithUV(x + 16, y, 100.0, 0.0625, 0.0);
222-
tessellator.addVertexWithUV(x, y, 100.0, 0.0, 0.0);
223-
tessellator.draw();
224-
}
225-
226216
}

src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/hudcaching/GuiIngameAccessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public interface GuiIngameAccessor {
1313
@Invoker
1414
void callRenderPumpkinBlur(int width, int height);
1515

16-
// render portal
17-
@Invoker
18-
void callFunc_130015_b(float partialTicks, int width, int height);
16+
@Invoker("func_130015_b")
17+
void callRenderPortal(float partialTicks, int width, int height);
1918
}

src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/hudcaching/MixinGuiIngameForge_HUDCaching.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.gtnewhorizons.angelica.mixins.early.angelica.hudcaching;
22

3-
import com.gtnewhorizons.angelica.hudcaching.HUDCaching;
4-
5-
import net.minecraft.client.gui.Gui;
6-
import net.minecraftforge.client.GuiIngameForge;
73
import org.spongepowered.asm.mixin.Mixin;
84
import org.spongepowered.asm.mixin.injection.At;
95
import org.spongepowered.asm.mixin.injection.Inject;
106
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
117

8+
import com.gtnewhorizons.angelica.hudcaching.HUDCaching;
9+
10+
import net.minecraft.client.gui.Gui;
11+
import net.minecraftforge.client.GuiIngameForge;
12+
1213
@Mixin(GuiIngameForge.class)
1314
public class MixinGuiIngameForge_HUDCaching {
1415
@Inject(method = "renderGameOverlay", at = @At("HEAD"))
@@ -25,6 +26,7 @@ public class MixinGuiIngameForge_HUDCaching {
2526
private void angelica$captureRenderCrosshair(CallbackInfo ci) {
2627
if (HUDCaching.renderingCacheOverride) {
2728
HUDCaching.renderCrosshairsCaptured = true;
29+
HUDCaching.fixGLStateBeforeRenderingCache();
2830
ci.cancel();
2931
}
3032
}

src/main/java/net/coderbot/iris/pipeline/HandRenderer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import net.minecraft.entity.player.EntityPlayer;
1111
import net.minecraft.item.Item;
1212
import net.minecraft.item.ItemBlock;
13+
import net.minecraft.item.ItemStack;
14+
1315
import org.joml.Matrix4f;
1416

1517
public class HandRenderer {
@@ -58,7 +60,11 @@ private boolean canRender(Camera camera, RenderGlobal gameRenderer) {
5860
public boolean isHandTranslucent(InteractionHand hand) {
5961
// TODO: Offhand
6062
// Item item = Minecraft.getMinecraft().thePlayer.getItemBySlot(hand == InteractionHand.OFF_HAND ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND).getItem();
61-
Item item = Minecraft.getMinecraft().thePlayer.getHeldItem().getItem();
63+
ItemStack heldItem = Minecraft.getMinecraft().thePlayer.getHeldItem();
64+
if (heldItem == null) {
65+
return false;
66+
}
67+
Item item = heldItem.getItem();
6268

6369
if (item instanceof ItemBlock itemBlock) {
6470
// TODO: RenderType

0 commit comments

Comments
 (0)