From 23f00d26bbccdaf1945bfc74d96aed8bcce17cb7 Mon Sep 17 00:00:00 2001 From: 50ap5ud5 <50ap5ud5125@gmail.com> Date: Tue, 9 Jun 2020 07:09:53 +0200 Subject: [PATCH] Backport of patch #6543 to enable Stencil buffer in net.minecraft.client.shader.Framebuffer. Closes #6751 --- .../client/shader/Framebuffer.java.patch | 47 +++++++++++++++++++ .../minecraftforge/testmods/StencilTest.java | 43 +++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 + 3 files changed, 92 insertions(+) create mode 100644 patches/minecraft/net/minecraft/client/shader/Framebuffer.java.patch create mode 100644 src/test/java/net/minecraftforge/testmods/StencilTest.java diff --git a/patches/minecraft/net/minecraft/client/shader/Framebuffer.java.patch b/patches/minecraft/net/minecraft/client/shader/Framebuffer.java.patch new file mode 100644 index 0000000000..91b71700b0 --- /dev/null +++ b/patches/minecraft/net/minecraft/client/shader/Framebuffer.java.patch @@ -0,0 +1,47 @@ +--- a/net/minecraft/client/shader/Framebuffer.java ++++ b/net/minecraft/client/shader/Framebuffer.java +@@ -95,8 +95,14 @@ + GLX.glFramebufferTexture2D(GLX.GL_FRAMEBUFFER, GLX.GL_COLOR_ATTACHMENT0, 3553, this.field_147617_g, 0); + if (this.field_147619_e) { + GLX.glBindRenderbuffer(GLX.GL_RENDERBUFFER, this.field_147624_h); ++ if (!stencilEnabled) { + GLX.glRenderbufferStorage(GLX.GL_RENDERBUFFER, 33190, this.field_147622_a, this.field_147620_b); + GLX.glFramebufferRenderbuffer(GLX.GL_FRAMEBUFFER, GLX.GL_DEPTH_ATTACHMENT, GLX.GL_RENDERBUFFER, this.field_147624_h); ++ } else { ++ GLX.glRenderbufferStorage(GLX.GL_RENDERBUFFER, org.lwjgl.opengl.EXTPackedDepthStencil.GL_DEPTH24_STENCIL8_EXT, this.field_147622_a, this.field_147620_b); ++ GLX.glFramebufferRenderbuffer(GLX.GL_FRAMEBUFFER, GLX.GL_DEPTH_ATTACHMENT, GLX.GL_RENDERBUFFER, this.field_147624_h); ++ GLX.glFramebufferRenderbuffer(GLX.GL_FRAMEBUFFER, org.lwjgl.opengl.EXTFramebufferObject.GL_STENCIL_ATTACHMENT_EXT, GLX.GL_RENDERBUFFER, this.field_147624_h); ++ } + } + + this.func_147611_b(); +@@ -229,4 +235,29 @@ + GlStateManager.clear(i, p_216493_1_); + this.func_147609_e(); + } ++ ++ /*================================ FORGE START ================================================*/ ++ private boolean stencilEnabled = false; ++ /** ++ * Attempts to enable 8 bits of stencil buffer on this FrameBuffer. ++ * Modders must call this directly to set things up. ++ * This is to prevent the default cause where graphics cards do not support stencil bits. ++ * Make sure to call this on the main render thread! ++ */ ++ public void enableStencil() ++ { ++ if(stencilEnabled) return; ++ stencilEnabled = true; ++ this.func_216491_a(field_147621_c, field_147618_d, net.minecraft.client.Minecraft.field_142025_a); ++ } ++ ++ /** ++ * Returns whether or not this FBO has been successfully initialised with stencil bits. ++ * If not, and a modder wishes it to be, they must call enableStencil. ++ */ ++ public boolean isStencilEnabled() ++ { ++ return this.stencilEnabled; ++ } ++ /*================================ FORGE END ================================================*/ + } diff --git a/src/test/java/net/minecraftforge/testmods/StencilTest.java b/src/test/java/net/minecraftforge/testmods/StencilTest.java new file mode 100644 index 0000000000..d98993f153 --- /dev/null +++ b/src/test/java/net/minecraftforge/testmods/StencilTest.java @@ -0,0 +1,43 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.testmods; + +import net.minecraft.client.Minecraft; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.fml.DeferredWorkQueue; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; + +@SuppressWarnings("deprecation") +@Mod(StencilTest.MODID) +public class StencilTest { + + public static final String MODID = "stencil_backport_test"; + + public StencilTest() { + IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); + modEventBus.addListener(this::clientSetup); + } + + private void clientSetup(FMLClientSetupEvent event) { + DeferredWorkQueue.runLater(() -> Minecraft.getInstance().getFramebuffer().enableStencil()); + } +} \ No newline at end of file diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index f6d44c6e45..416261629c 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -69,3 +69,5 @@ loaderVersion="[28,)" modId="custom_plant_type_test" [[mods]] modId="forgedebugmultilayermodel" +[[mods]] + modId="stencil_backport_test"