From ea3aeb45a823cc3e2f5b5695eac0c2cc2ad11971 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Wed, 27 Nov 2024 17:19:44 +0000 Subject: [PATCH 01/35] Separate ClientModLoader begin/finish into two methods, implement MainTarget configuration event --- .../blaze3d/pipeline/MainTarget.java.patch | 15 ++ .../blaze3d/pipeline/RenderTarget.java.patch | 134 ++++++++++++++++++ .../net/minecraft/client/Minecraft.java.patch | 8 +- .../neoforge/client/ClientHooks.java | 7 + .../event/ConfigureMainRenderTargetEvent.java | 72 ++++++++++ .../client/loading/ClientModLoader.java | 5 +- 6 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch create mode 100644 patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch create mode 100644 src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch new file mode 100644 index 00000000000..8071336f342 --- /dev/null +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -0,0 +1,15 @@ +--- a/com/mojang/blaze3d/pipeline/MainTarget.java ++++ b/com/mojang/blaze3d/pipeline/MainTarget.java +@@ -16,7 +_,11 @@ + static final MainTarget.Dimension DEFAULT_DIMENSIONS = new MainTarget.Dimension(854, 480); + + public MainTarget(int p_166137_, int p_166138_) { +- super(true); ++ this(net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(), p_166137_, p_166138_); ++ } ++ ++ private MainTarget(net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent e, int p_166137_, int p_166138_) { ++ super(e.useDepth(), e.useStencil()); + this.createFrameBuffer(p_166137_, p_166138_); + } + diff --git a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch new file mode 100644 index 00000000000..bddf05a9c12 --- /dev/null +++ b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch @@ -0,0 +1,134 @@ +--- a/com/mojang/blaze3d/pipeline/RenderTarget.java ++++ b/com/mojang/blaze3d/pipeline/RenderTarget.java +@@ -25,17 +_,31 @@ + public int viewWidth; + public int viewHeight; + public final boolean useDepth; ++ public final boolean useStencil; + public int frameBufferId; + protected int colorTextureId; + protected int depthBufferId; ++ protected int stencilBufferId; + private final float[] clearChannels = Util.make(() -> new float[]{1.0F, 1.0F, 1.0F, 0.0F}); + public int filterMode; + + public RenderTarget(boolean p_166199_) { +- this.useDepth = p_166199_; ++ this(p_166199_, false); ++ } ++ ++ public RenderTarget(boolean useDepth, boolean useStencil) { ++ this.useDepth = useDepth; ++ this.useStencil = useStencil; + this.frameBufferId = -1; + this.colorTextureId = -1; + this.depthBufferId = -1; ++ ++ if (!useDepth && useStencil) { ++ var capabilities = org.lwjgl.opengl.GL.getCapabilities(); ++ if (!capabilities.GL_ARB_texture_stencil8 && !capabilities.OpenGL44) { ++ throw new UnsupportedOperationException("Stencil-only buffers require GL_ARB_texture_stencil8 OR OpenGL 4.4"); ++ } ++ } + } + + public void resize(int p_83942_, int p_83943_) { +@@ -53,6 +_,11 @@ + RenderSystem.assertOnRenderThreadOrInit(); + this.unbindRead(); + this.unbindWrite(); ++ if (this.stencilBufferId > -1 && this.stencilBufferId != this.depthBufferId) { ++ TextureUtil.releaseTextureId(this.stencilBufferId);; ++ this.stencilBufferId = -1; ++ } ++ + if (this.depthBufferId > -1) { + TextureUtil.releaseTextureId(this.depthBufferId); + this.depthBufferId = -1; +@@ -96,9 +_,50 @@ + GlStateManager._texParameter(3553, 34892, 0); + GlStateManager._texParameter(3553, 10242, 33071); + GlStateManager._texParameter(3553, 10243, 33071); ++ if (!this.useStencil) // If stenciling is enabled, we will fill this later + GlStateManager._texImage2D(3553, 0, 6402, this.width, this.height, 0, 6402, 5126, null); + } + ++ if (this.useStencil) { ++ if (this.useDepth) { ++ // If depth and stencil buffers are both enabled, we must combine them ++ this.stencilBufferId = this.depthBufferId; ++ } else { ++ // Otherwise, we can generate a new texture in its place. ++ this.stencilBufferId = TextureUtil.generateTextureId(); ++ GlStateManager._bindTexture(this.stencilBufferId); ++ GlStateManager._texParameter(3553, 10241, 9728); ++ GlStateManager._texParameter(3553, 10240, 9728); ++ GlStateManager._texParameter(3553, 34892, 0); ++ GlStateManager._texParameter(3553, 10242, 33071); ++ GlStateManager._texParameter(3553, 10243, 33071); ++ } ++ ++ if (this.useDepth) { ++ // Use a combined format for both depth and stencil. ++ GlStateManager._texImage2D( ++ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, ++ 0, ++ org.lwjgl.opengl.GL32.GL_DEPTH24_STENCIL8, ++ this.width, this.height, ++ 0, ++ org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, ++ org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, ++ null); ++ } else { ++ // Otherwise, we can use a separate format. Testing for this was done in the constructor already. ++ GlStateManager._texImage2D( ++ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, ++ 0, ++ org.lwjgl.opengl.GL32.GL_STENCIL_INDEX8, ++ this.width, this.height, ++ 0, ++ org.lwjgl.opengl.GL32.GL_STENCIL_INDEX, ++ org.lwjgl.opengl.GL32.GL_BYTE, ++ null); ++ } ++ } ++ + this.setFilterMode(9728, true); + GlStateManager._bindTexture(this.colorTextureId); + GlStateManager._texParameter(3553, 10242, 33071); +@@ -109,6 +_,14 @@ + if (this.useDepth) { + GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); + } ++ if (this.useStencil) { ++ GlStateManager._glFramebufferTexture2D( ++ org.lwjgl.opengl.GL32.GL_FRAMEBUFFER, ++ org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, ++ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, ++ this.stencilBufferId, ++ 0); ++ } + + this.checkStatus(); + this.clear(); +@@ -218,6 +_,10 @@ + GlStateManager._clearDepth(1.0); + i |= 256; + } ++ if (this.useStencil) { ++ GlStateManager._clearStencil(0); ++ i |= org.lwjgl.opengl.GL32.GL_STENCIL_BUFFER_BIT; ++ } + + GlStateManager._clear(i); + this.unbindWrite(); +@@ -229,5 +_,9 @@ + + public int getDepthTextureId() { + return this.depthBufferId; ++ } ++ ++ public int getStencilBufferId() { ++ return this.stencilBufferId; + } + } diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 04429bcce30..4b3dc587ed0 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -8,7 +8,7 @@ this.demo = p_91084_.game.demo; this.allowsMultiplayer = !p_91084_.game.disableMultiplayer; this.allowsChat = !p_91084_.game.disableChat; -@@ -483,15 +_,17 @@ +@@ -483,15 +_,18 @@ LOGGER.error("Couldn't set icon", (Throwable)ioexception); } @@ -18,11 +18,12 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); ++ net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); this.mainRenderTarget.clear(); this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); -+ net.neoforged.neoforge.client.loading.ClientModLoader.begin(this, this.resourcePackRepository, this.resourceManager); ++ net.neoforged.neoforge.client.loading.ClientModLoader.finish(this.resourcePackRepository, this.resourceManager); + //Move client bootstrap to after mod loading so that events can be fired for it. + ClientBootstrap.bootstrap(); this.resourcePackRepository.reload(); @@ -177,7 +178,7 @@ this.deltaTracker.updatePauseState(this.pause); this.deltaTracker.updateFrozenState(!this.isLevelRunningNormally()); long l = Util.getNanos(); -@@ -1351,10 +_,12 @@ +@@ -1351,10 +_,13 @@ this.window.setGuiScale((double)i); if (this.screen != null) { this.screen.resize(this, this.window.getGuiScaledWidth(), this.window.getGuiScaledHeight()); @@ -185,6 +186,7 @@ } RenderTarget rendertarget = this.getMainRenderTarget(); ++ if (rendertarget != null) rendertarget.resize(this.window.getWidth(), this.window.getHeight()); + if (this.gameRenderer != null) this.gameRenderer.resize(this.window.getWidth(), this.window.getHeight()); diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index 7e497ecaf27..bd56d1fe9e8 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -144,6 +144,7 @@ import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; import net.neoforged.neoforge.client.event.ClientTickEvent; import net.neoforged.neoforge.client.event.ComputeFovModifierEvent; +import net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent; import net.neoforged.neoforge.client.event.CustomizeGuiOverlayEvent; import net.neoforged.neoforge.client.event.EntityRenderersEvent; import net.neoforged.neoforge.client.event.FrameGraphSetupEvent; @@ -1099,4 +1100,10 @@ public static Map gatherMaterialAtlases(Map< public static FrameGraphSetupEvent fireFrameGraphSetup(FrameGraphBuilder builder, LevelTargetBundle targets, RenderTargetDescriptor renderTargetDescriptor, Frustum frustum, Camera camera, Matrix4f modelViewMatrix, Matrix4f projectionMatrix, DeltaTracker deltaTracker, ProfilerFiller profiler) { return NeoForge.EVENT_BUS.post(new FrameGraphSetupEvent(builder, targets, renderTargetDescriptor, frustum, camera, modelViewMatrix, projectionMatrix, deltaTracker, profiler)); } + + public static ConfigureMainRenderTargetEvent configureMainRenderTarget() { + var e = new ConfigureMainRenderTargetEvent(); + ModLoader.postEvent(e); + return e; + } } diff --git a/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java new file mode 100644 index 00000000000..c4bc57ad89e --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.client.event; + +import com.mojang.blaze3d.pipeline.MainTarget; +import com.mojang.blaze3d.pipeline.RenderTarget; +import net.neoforged.bus.api.Event; +import net.neoforged.bus.api.ICancellableEvent; +import net.neoforged.fml.LogicalSide; +import net.neoforged.fml.event.IModBusEvent; +import org.jetbrains.annotations.ApiStatus; + +/** + * Fired when configuring the main {@linkplain RenderTarget render target}. + *

+ * This event fires during startup when the {@link MainTarget} is constructed. + *

+ * This event is not {@linkplain ICancellableEvent cancellable}. + *

+ * This event is fired on the mod-speciffic event bus, only on the {@linkplain LogicalSide#CLIENT logical client}. + */ +public class ConfigureMainRenderTargetEvent extends Event implements IModBusEvent { + private boolean useDepth; + private boolean useStencil; + + @ApiStatus.Internal + public ConfigureMainRenderTargetEvent() { + this.useDepth = true; + this.useStencil = false; + } + + /** + * Returns whether the depth buffer is enabled. + * + * @return true, if the depth buffer is enabled, or false otherwise. + */ + public boolean useDepth() { + return this.useDepth; + } + + /** + * Returns whether the stencil buffer is enabled. + * + * @return true, if the stencil buffer is enabled, or false otherwise. + */ + public boolean useStencil() { + return this.useStencil; + } + + /** + * Enable the depth buffer for the main render target. + * + * @return this, for method chaining. + */ + public ConfigureMainRenderTargetEvent enableDepth() { + this.useDepth = true; + return this; + } + + /** + * Enable the stencil buffer for the main render target. + * + * @return this, for method chaining. + */ + public ConfigureMainRenderTargetEvent enableStencil() { + this.useStencil = true; + return this; + } +} diff --git a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java index d8c4a35e616..4ee4d48b270 100644 --- a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java +++ b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java @@ -47,7 +47,7 @@ public class ClientModLoader extends CommonModLoader { @Nullable private static ModLoadingException error; - public static void begin(final Minecraft minecraft, final PackRepository defaultResourcePacks, final ReloadableResourceManager mcResourceManager) { + public static void begin(final Minecraft minecraft) { // force log4j to shutdown logging in a shutdown hook. This is because we disable default shutdown hook so the server properly logs it's shutdown Runtime.getRuntime().addShutdownHook(new Thread(LogManager::shutdown)); ImmediateWindowHandler.updateProgress("Loading mods"); @@ -60,6 +60,9 @@ public static void begin(final Minecraft minecraft, final PackRepository default } catch (ModLoadingException e) { error = e; } + } + + public static void finish(final PackRepository defaultResourcePacks, final ReloadableResourceManager mcResourceManager) { if (error == null) { ResourcePackLoader.populatePackRepository(defaultResourcePacks, PackType.CLIENT_RESOURCES, false); DataPackConfig.DEFAULT.addModPacks(ResourcePackLoader.getPackNames(PackType.SERVER_DATA)); From 5135ec123db09b3f85bea5e840adf16d624ba416 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Sat, 30 Nov 2024 18:38:55 +0000 Subject: [PATCH 02/35] Flow stencil information through post chains --- .../framegraph/FrameGraphBuilder.java.patch | 46 ++++++++++++++++++ .../blaze3d/pipeline/RenderTarget.java.patch | 2 +- .../blaze3d/pipeline/TextureTarget.java.patch | 14 ++++++ .../RenderTargetDescriptor.java.patch | 18 +++++++ .../client/renderer/LevelRenderer.java.patch | 10 ++++ .../client/renderer/PostChain.java.patch | 48 +++++++++++++++++++ .../renderer/PostChainConfig.java.patch | 23 +++++++++ .../client/renderer/PostPass.java.patch | 28 +++++++++++ .../resources/META-INF/accesstransformer.cfg | 4 ++ 9 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch create mode 100644 patches/com/mojang/blaze3d/pipeline/TextureTarget.java.patch create mode 100644 patches/com/mojang/blaze3d/resource/RenderTargetDescriptor.java.patch create mode 100644 patches/net/minecraft/client/renderer/PostChain.java.patch create mode 100644 patches/net/minecraft/client/renderer/PostChainConfig.java.patch create mode 100644 patches/net/minecraft/client/renderer/PostPass.java.patch diff --git a/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch b/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch new file mode 100644 index 00000000000..b03f4d9e054 --- /dev/null +++ b/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch @@ -0,0 +1,46 @@ +--- a/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java ++++ b/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java +@@ -173,6 +_,11 @@ + public T get() { + return this.resource; + } ++ ++ @Override ++ public ResourceDescriptor getDescriptor() { ++ return null; ++ } + } + + @OnlyIn(Dist.CLIENT) +@@ -211,6 +_,10 @@ + public String toString() { + return this.createdBy != null ? this.holder + "#" + this.version + " (from " + this.createdBy + ")" : this.holder + "#" + this.version; + } ++ ++ public ResourceDescriptor getDescriptor() { ++ return this.holder.getDescriptor(); ++ } + } + + @OnlyIn(Dist.CLIENT) +@@ -265,6 +_,11 @@ + this.physicalResource = null; + } + } ++ ++ @Override ++ public ResourceDescriptor getDescriptor() { ++ return descriptor; ++ } + } + + @OnlyIn(Dist.CLIENT) +@@ -364,5 +_,8 @@ + public String toString() { + return this.name; + } ++ ++ @Nullable ++ public abstract ResourceDescriptor getDescriptor(); + } + } diff --git a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch index bddf05a9c12..b4990074aef 100644 --- a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch @@ -128,7 +128,7 @@ return this.depthBufferId; + } + -+ public int getStencilBufferId() { ++ public int getStencilTextureId() { + return this.stencilBufferId; } } diff --git a/patches/com/mojang/blaze3d/pipeline/TextureTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/TextureTarget.java.patch new file mode 100644 index 00000000000..e0661d83e49 --- /dev/null +++ b/patches/com/mojang/blaze3d/pipeline/TextureTarget.java.patch @@ -0,0 +1,14 @@ +--- a/com/mojang/blaze3d/pipeline/TextureTarget.java ++++ b/com/mojang/blaze3d/pipeline/TextureTarget.java +@@ -7,7 +_,10 @@ + @OnlyIn(Dist.CLIENT) + public class TextureTarget extends RenderTarget { + public TextureTarget(int p_166213_, int p_166214_, boolean p_166215_) { +- super(p_166215_); ++ this(p_166213_, p_166214_, p_166215_, false); ++ } ++ public TextureTarget(int p_166213_, int p_166214_, boolean p_166215_, boolean useStencil) { ++ super(p_166215_, useStencil); + RenderSystem.assertOnRenderThreadOrInit(); + this.resize(p_166213_, p_166214_); + } diff --git a/patches/com/mojang/blaze3d/resource/RenderTargetDescriptor.java.patch b/patches/com/mojang/blaze3d/resource/RenderTargetDescriptor.java.patch new file mode 100644 index 00000000000..060e486b3c7 --- /dev/null +++ b/patches/com/mojang/blaze3d/resource/RenderTargetDescriptor.java.patch @@ -0,0 +1,18 @@ +--- a/com/mojang/blaze3d/resource/RenderTargetDescriptor.java ++++ b/com/mojang/blaze3d/resource/RenderTargetDescriptor.java +@@ -6,9 +_,13 @@ + import net.neoforged.api.distmarker.OnlyIn; + + @OnlyIn(Dist.CLIENT) +-public record RenderTargetDescriptor(int width, int height, boolean useDepth) implements ResourceDescriptor { ++public record RenderTargetDescriptor(int width, int height, boolean useDepth, boolean useStencil) implements ResourceDescriptor { ++ public RenderTargetDescriptor(int width, int height, boolean useDepth) { ++ this(width, height, useDepth, false); ++ } ++ + public RenderTarget allocate() { +- return new TextureTarget(this.width, this.height, this.useDepth); ++ return new TextureTarget(this.width, this.height, this.useDepth, this.useStencil); + } + + public void free(RenderTarget p_363223_) { diff --git a/patches/net/minecraft/client/renderer/LevelRenderer.java.patch b/patches/net/minecraft/client/renderer/LevelRenderer.java.patch index 9173efdbf43..094b3d9dfee 100644 --- a/patches/net/minecraft/client/renderer/LevelRenderer.java.patch +++ b/patches/net/minecraft/client/renderer/LevelRenderer.java.patch @@ -1,5 +1,15 @@ --- a/net/minecraft/client/renderer/LevelRenderer.java +++ b/net/minecraft/client/renderer/LevelRenderer.java +@@ -459,7 +_,8 @@ + this.targets.main = framegraphbuilder.importExternal("main", this.minecraft.getMainRenderTarget()); + int i = this.minecraft.getMainRenderTarget().width; + int j = this.minecraft.getMainRenderTarget().height; +- RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true); ++ boolean useStencil = this.minecraft.getMainRenderTarget().useStencil; ++ RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true, useStencil); + PostChain postchain = this.getTransparencyChain(); + if (postchain != null) { + this.targets.translucent = framegraphbuilder.createInternal("translucent", rendertargetdescriptor); @@ -473,6 +_,9 @@ this.targets.entityOutline = framegraphbuilder.importExternal("entity_outline", this.entityOutlineTarget); } diff --git a/patches/net/minecraft/client/renderer/PostChain.java.patch b/patches/net/minecraft/client/renderer/PostChain.java.patch new file mode 100644 index 00000000000..0b08462b4f6 --- /dev/null +++ b/patches/net/minecraft/client/renderer/PostChain.java.patch @@ -0,0 +1,48 @@ +--- a/net/minecraft/client/renderer/PostChain.java ++++ b/net/minecraft/client/renderer/PostChain.java +@@ -79,8 +_,8 @@ + abstracttexture.setFilter(flag, false); + postpass.addInput(new PostPass.TextureInput(s3, abstracttexture, i, j)); + continue; +- case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2): +- postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, flag1, flag2)); ++ case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2, boolean useStencilBuffer): ++ postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, flag1, flag2, useStencilBuffer)); + continue; + default: + throw new MatchException(null, null); +@@ -95,17 +_,32 @@ + Matrix4f matrix4f = new Matrix4f().setOrtho(0.0F, (float)p_361423_, 0.0F, (float)p_362735_, 0.1F, 1000.0F); + Map> map = new HashMap<>(this.internalTargets.size() + this.externalTargets.size()); + ++ // Enable the depth and stencil buffers based on whether any external targets use them. ++ // This is necessary so any created buffers get the correct parameters for blitting. ++ boolean useDepth = false; ++ boolean useStencil = false; + for (ResourceLocation resourcelocation : this.externalTargets) { + map.put(resourcelocation, p_361871_.getOrThrow(resourcelocation)); ++ ++ var handle = p_361871_.get(resourcelocation); ++ ++ if (handle instanceof FrameGraphBuilder.Handle frameHandle ++ && frameHandle.getDescriptor() instanceof RenderTargetDescriptor renderDescriptor) { ++ useDepth |= renderDescriptor.useDepth(); ++ useStencil |= renderDescriptor.useStencil(); ++ } else { ++ useDepth |= p_361871_.get(resourcelocation).get().useDepth; ++ useStencil |= p_361871_.get(resourcelocation).get().useStencil; ++ } + } + + for (Entry entry : this.internalTargets.entrySet()) { + ResourceLocation resourcelocation1 = entry.getKey(); + RenderTargetDescriptor rendertargetdescriptor = switch (entry.getValue()) { + case PostChainConfig.FixedSizedTarget(int i, int j) -> { +- yield new RenderTargetDescriptor(i, j, true); ++ yield new RenderTargetDescriptor(i, j, useDepth, useStencil); + } +- case PostChainConfig.FullScreenTarget postchainconfig$fullscreentarget -> new RenderTargetDescriptor(p_361423_, p_362735_, true); ++ case PostChainConfig.FullScreenTarget postchainconfig$fullscreentarget -> new RenderTargetDescriptor(p_361423_, p_362735_, useDepth, useStencil); + default -> throw new MatchException(null, null); + }; + map.put(resourcelocation1, p_362523_.createInternal(resourcelocation1.toString(), rendertargetdescriptor)); diff --git a/patches/net/minecraft/client/renderer/PostChainConfig.java.patch b/patches/net/minecraft/client/renderer/PostChainConfig.java.patch new file mode 100644 index 00000000000..e07b4d10af6 --- /dev/null +++ b/patches/net/minecraft/client/renderer/PostChainConfig.java.patch @@ -0,0 +1,23 @@ +--- a/net/minecraft/client/renderer/PostChainConfig.java ++++ b/net/minecraft/client/renderer/PostChainConfig.java +@@ -108,13 +_,18 @@ + } + + @OnlyIn(Dist.CLIENT) +- public static record TargetInput(String samplerName, ResourceLocation targetId, boolean useDepthBuffer, boolean bilinear) implements PostChainConfig.Input { ++ public static record TargetInput(String samplerName, ResourceLocation targetId, boolean useDepthBuffer, boolean bilinear, boolean useStencilBuffer) implements PostChainConfig.Input { ++ public TargetInput(String samplerName, ResourceLocation targetId, boolean useDepthBuffer, boolean bilinear) { ++ this(samplerName, targetId, useDepthBuffer, bilinear, false); ++ } ++ + public static final Codec CODEC = RecordCodecBuilder.create( + p_363892_ -> p_363892_.group( + Codec.STRING.fieldOf("sampler_name").forGetter(PostChainConfig.TargetInput::samplerName), + ResourceLocation.CODEC.fieldOf("target").forGetter(PostChainConfig.TargetInput::targetId), + Codec.BOOL.optionalFieldOf("use_depth_buffer", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::useDepthBuffer), +- Codec.BOOL.optionalFieldOf("bilinear", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::bilinear) ++ Codec.BOOL.optionalFieldOf("bilinear", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::bilinear), ++ Codec.BOOL.optionalFieldOf("neoforge:use_stencil_buffer", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::useStencilBuffer) + ) + .apply(p_363892_, PostChainConfig.TargetInput::new) + ); diff --git a/patches/net/minecraft/client/renderer/PostPass.java.patch b/patches/net/minecraft/client/renderer/PostPass.java.patch new file mode 100644 index 00000000000..5ef576cd68a --- /dev/null +++ b/patches/net/minecraft/client/renderer/PostPass.java.patch @@ -0,0 +1,28 @@ +--- a/net/minecraft/client/renderer/PostPass.java ++++ b/net/minecraft/client/renderer/PostPass.java +@@ -122,7 +_,10 @@ + } + + @OnlyIn(Dist.CLIENT) +- public static record TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear) implements PostPass.Input { ++ public static record TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear, boolean stencilBuffer) implements PostPass.Input { ++ public TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear) { ++ this(samplerName, targetId, depthBuffer, bilinear, false); ++ } + private ResourceHandle getHandle(Map> p_364534_) { + ResourceHandle resourcehandle = p_364534_.get(this.targetId); + if (resourcehandle == null) { +@@ -142,7 +_,12 @@ + ResourceHandle resourcehandle = this.getHandle(p_361239_); + RenderTarget rendertarget = resourcehandle.get(); + rendertarget.setFilterMode(this.bilinear ? 9729 : 9728); +- p_366564_.bindSampler(this.samplerName + "Sampler", this.depthBuffer ? rendertarget.getDepthTextureId() : rendertarget.getColorTextureId()); ++ if (this.depthBuffer) ++ p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getDepthTextureId()); ++ else if (this.stencilBuffer) ++ p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getStencilTextureId()); ++ else ++ p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getColorTextureId()); + p_366564_.safeGetUniform(this.samplerName + "Size").set((float)rendertarget.width, (float)rendertarget.height); + } + diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index fc21dedc592..1f4ce258fa3 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -1,4 +1,8 @@ # Note: This file is for manually added ATs. When AT entries can be programmatically generated based on fixed rules you may define those rules in the build.gradle file +public com.mojang.blaze3d.framegraph.FrameGraphBuilder$Handle +public com.mojang.blaze3d.framegraph.FrameGraphBuilder$Handle holder +public com.mojang.blaze3d.framegraph.FrameGraphBuilder$VirtualResource +public com.mojang.blaze3d.framegraph.FrameGraphBuilder$Pass public net.minecraft.advancements.CriteriaTriggers register(Ljava/lang/String;Lnet/minecraft/advancements/CriterionTrigger;)Lnet/minecraft/advancements/CriterionTrigger; # register default net.minecraft.client.KeyMapping isDown # isDown public-f net.minecraft.client.Options keyMappings # keyMappings From 613cb46eecb8bd9c8b737106b6a5be31084e09be Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Sun, 8 Dec 2024 14:51:21 +0000 Subject: [PATCH 03/35] Fix copy-paste fail in RenderTarget --- patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch index b4990074aef..bca255906d1 100644 --- a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch @@ -38,7 +38,7 @@ this.unbindRead(); this.unbindWrite(); + if (this.stencilBufferId > -1 && this.stencilBufferId != this.depthBufferId) { -+ TextureUtil.releaseTextureId(this.stencilBufferId);; ++ TextureUtil.releaseTextureId(this.stencilBufferId); + this.stencilBufferId = -1; + } + From f938bfe32ddbd5b0d93a3fb16970b81662c18b09 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Sun, 8 Dec 2024 14:59:49 +0000 Subject: [PATCH 04/35] Use an enum for the buffer type instead of two bools --- .../client/renderer/PostChain.java.patch | 11 +++- .../client/renderer/PostPass.java.patch | 57 +++++++++++++++++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/patches/net/minecraft/client/renderer/PostChain.java.patch b/patches/net/minecraft/client/renderer/PostChain.java.patch index 0b08462b4f6..73b227ac22b 100644 --- a/patches/net/minecraft/client/renderer/PostChain.java.patch +++ b/patches/net/minecraft/client/renderer/PostChain.java.patch @@ -1,5 +1,14 @@ --- a/net/minecraft/client/renderer/PostChain.java +++ b/net/minecraft/client/renderer/PostChain.java +@@ -17,6 +_,8 @@ + import java.util.stream.Collectors; + import java.util.stream.Stream; + import javax.annotation.Nullable; ++ ++import net.minecraft.client.renderer.PostPass.TargetInput.BufferType; + import net.minecraft.client.renderer.texture.AbstractTexture; + import net.minecraft.client.renderer.texture.TextureManager; + import net.minecraft.resources.ResourceLocation; @@ -79,8 +_,8 @@ abstracttexture.setFilter(flag, false); postpass.addInput(new PostPass.TextureInput(s3, abstracttexture, i, j)); @@ -7,7 +16,7 @@ - case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2): - postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, flag1, flag2)); + case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2, boolean useStencilBuffer): -+ postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, flag1, flag2, useStencilBuffer)); ++ postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, BufferType.from(flag1, useStencilBuffer), flag2)); continue; default: throw new MatchException(null, null); diff --git a/patches/net/minecraft/client/renderer/PostPass.java.patch b/patches/net/minecraft/client/renderer/PostPass.java.patch index 5ef576cd68a..efa98f666ba 100644 --- a/patches/net/minecraft/client/renderer/PostPass.java.patch +++ b/patches/net/minecraft/client/renderer/PostPass.java.patch @@ -1,25 +1,70 @@ --- a/net/minecraft/client/renderer/PostPass.java +++ b/net/minecraft/client/renderer/PostPass.java -@@ -122,7 +_,10 @@ +@@ -122,7 +_,54 @@ } @OnlyIn(Dist.CLIENT) - public static record TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear) implements PostPass.Input { -+ public static record TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear, boolean stencilBuffer) implements PostPass.Input { ++ public static record TargetInput(String samplerName, ResourceLocation targetId, BufferType bufferType, boolean bilinear) implements PostPass.Input { ++ public enum BufferType { ++ NONE(false, false), ++ DEPTH_ONLY(true, false), ++ STENCIL_ONLY(false, true), ++ DEPTH_STENCIL(true, true); ++ ++ private final boolean depth; ++ private final boolean stencil; ++ ++ BufferType(boolean depth, boolean stencil) { ++ this.depth = depth; ++ this.stencil = stencil; ++ } ++ ++ public boolean hasDepth() { ++ return this.depth; ++ } ++ ++ public boolean hasStencil() { ++ return this.stencil; ++ } ++ ++ public static BufferType from(boolean useDepth, boolean useStencil) { ++ if (useDepth && useStencil) { ++ return DEPTH_STENCIL; ++ } else if (useDepth) { ++ return DEPTH_ONLY; ++ } else if (useStencil) { ++ return STENCIL_ONLY; ++ } else { ++ return NONE; ++ } ++ } ++ } ++ + public TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear) { -+ this(samplerName, targetId, depthBuffer, bilinear, false); ++ this(samplerName, targetId, depthBuffer ? BufferType.DEPTH_ONLY : BufferType.NONE, bilinear); ++ } ++ ++ public boolean depthBuffer() { ++ return bufferType.hasDepth(); ++ } ++ ++ public boolean stencilBuffer() { ++ return bufferType.hasStencil(); + } ++ private ResourceHandle getHandle(Map> p_364534_) { ResourceHandle resourcehandle = p_364534_.get(this.targetId); if (resourcehandle == null) { -@@ -142,7 +_,12 @@ +@@ -142,7 +_,13 @@ ResourceHandle resourcehandle = this.getHandle(p_361239_); RenderTarget rendertarget = resourcehandle.get(); rendertarget.setFilterMode(this.bilinear ? 9729 : 9728); - p_366564_.bindSampler(this.samplerName + "Sampler", this.depthBuffer ? rendertarget.getDepthTextureId() : rendertarget.getColorTextureId()); -+ if (this.depthBuffer) ++ if (this.depthBuffer()) + p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getDepthTextureId()); -+ else if (this.stencilBuffer) ++ // If stencil is specified ++ else if (this.stencilBuffer()) + p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getStencilTextureId()); + else + p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getColorTextureId()); From b5cd630c834a8df3d9a1fcd16e843d15dd00f701 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Sun, 8 Dec 2024 15:11:53 +0000 Subject: [PATCH 05/35] Fix import whoopsie --- .../minecraft/client/renderer/PostChain.java.patch | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/patches/net/minecraft/client/renderer/PostChain.java.patch b/patches/net/minecraft/client/renderer/PostChain.java.patch index 73b227ac22b..e0c65d1b7ed 100644 --- a/patches/net/minecraft/client/renderer/PostChain.java.patch +++ b/patches/net/minecraft/client/renderer/PostChain.java.patch @@ -1,14 +1,5 @@ --- a/net/minecraft/client/renderer/PostChain.java +++ b/net/minecraft/client/renderer/PostChain.java -@@ -17,6 +_,8 @@ - import java.util.stream.Collectors; - import java.util.stream.Stream; - import javax.annotation.Nullable; -+ -+import net.minecraft.client.renderer.PostPass.TargetInput.BufferType; - import net.minecraft.client.renderer.texture.AbstractTexture; - import net.minecraft.client.renderer.texture.TextureManager; - import net.minecraft.resources.ResourceLocation; @@ -79,8 +_,8 @@ abstracttexture.setFilter(flag, false); postpass.addInput(new PostPass.TextureInput(s3, abstracttexture, i, j)); @@ -16,7 +7,7 @@ - case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2): - postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, flag1, flag2)); + case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2, boolean useStencilBuffer): -+ postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, BufferType.from(flag1, useStencilBuffer), flag2)); ++ postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, net.minecraft.client.renderer.PostPass.TargetInput.BufferType.from(flag1, useStencilBuffer), flag2)); continue; default: throw new MatchException(null, null); From 922fad6940c5c6ce72491be510486dfcb22240c7 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Mon, 9 Dec 2024 09:26:28 +0000 Subject: [PATCH 06/35] Fix stenciling on the main target --- .../blaze3d/pipeline/MainTarget.java.patch | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 8071336f342..7e4a944801e 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -13,3 +13,127 @@ this.createFrameBuffer(p_166137_, p_166138_); } +@@ -30,13 +_,24 @@ + GlStateManager._texParameter(3553, 10242, 33071); + GlStateManager._texParameter(3553, 10243, 33071); + GlStateManager._glFramebufferTexture2D(36160, 36064, 3553, this.colorTextureId, 0); +- GlStateManager._bindTexture(this.depthBufferId); +- GlStateManager._texParameter(3553, 34892, 0); +- GlStateManager._texParameter(3553, 10241, 9728); +- GlStateManager._texParameter(3553, 10240, 9728); +- GlStateManager._texParameter(3553, 10242, 33071); +- GlStateManager._texParameter(3553, 10243, 33071); +- GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); ++ if (this.useDepth) { ++ GlStateManager._bindTexture(this.depthBufferId); ++ GlStateManager._texParameter(3553, 34892, 0); ++ GlStateManager._texParameter(3553, 10241, 9728); ++ GlStateManager._texParameter(3553, 10240, 9728); ++ GlStateManager._texParameter(3553, 10242, 33071); ++ GlStateManager._texParameter(3553, 10243, 33071); ++ GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); ++ } ++ if (this.useStencil) { ++ GlStateManager._bindTexture(this.stencilBufferId); ++ GlStateManager._texParameter(3553, 34892, 0); ++ GlStateManager._texParameter(3553, 10241, 9728); ++ GlStateManager._texParameter(3553, 10240, 9728); ++ GlStateManager._texParameter(3553, 10242, 33071); ++ GlStateManager._texParameter(3553, 10243, 33071); ++ GlStateManager._glFramebufferTexture2D(36160, org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, 3553, this.stencilBufferId, 0); ++ } + GlStateManager._bindTexture(0); + this.viewWidth = maintarget$dimension.width; + this.viewHeight = maintarget$dimension.height; +@@ -49,8 +_,14 @@ + private MainTarget.Dimension allocateAttachments(int p_166147_, int p_166148_) { + RenderSystem.assertOnRenderThreadOrInit(); + this.colorTextureId = TextureUtil.generateTextureId(); +- this.depthBufferId = TextureUtil.generateTextureId(); ++ if (this.useDepth) { ++ this.depthBufferId = TextureUtil.generateTextureId(); ++ } ++ if (this.useStencil) { ++ this.stencilBufferId = this.useDepth ? this.depthBufferId : TextureUtil.generateTextureId(); ++ } + MainTarget.AttachmentState maintarget$attachmentstate = MainTarget.AttachmentState.NONE; ++ MainTarget.AttachmentState targetState = MainTarget.AttachmentState.of(true, this.useDepth, this.useStencil); + + for (MainTarget.Dimension maintarget$dimension : MainTarget.Dimension.listWithFallback(p_166147_, p_166148_)) { + maintarget$attachmentstate = MainTarget.AttachmentState.NONE; +@@ -58,11 +_,19 @@ + maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.COLOR); + } + +- if (this.allocateDepthAttachment(maintarget$dimension)) { ++ if (this.useDepth && this.useStencil && this.allocateDepthStencilAttachment(maintarget$dimension)) { ++ maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH_STENCIL); ++ } ++ ++ else if (this.useDepth && this.allocateDepthAttachment(maintarget$dimension)) { + maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH); + } + +- if (maintarget$attachmentstate == MainTarget.AttachmentState.COLOR_DEPTH) { ++ else if (this.useStencil && this.allocateStencilAttachment(maintarget$dimension)) { ++ maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.STENCIL); ++ } ++ ++ if (maintarget$attachmentstate == targetState) { + return maintarget$dimension; + } + } +@@ -86,17 +_,52 @@ + return GlStateManager._getError() != 1285; + } + ++ private boolean allocateStencilAttachment(MainTarget.Dimension p_166145_) { ++ RenderSystem.assertOnRenderThreadOrInit(); ++ GlStateManager._getError(); ++ GlStateManager._bindTexture(this.stencilBufferId); ++ GlStateManager._texImage2D(3553, 0, org.lwjgl.opengl.GL32.GL_STENCIL_INDEX8, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_STENCIL_INDEX, org.lwjgl.opengl.GL32.GL_BYTE, null); ++ return GlStateManager._getError() != 1285; ++ } ++ ++ private boolean allocateDepthStencilAttachment(MainTarget.Dimension p_166145_) { ++ RenderSystem.assertOnRenderThreadOrInit(); ++ GlStateManager._getError(); ++ GlStateManager._bindTexture(this.depthBufferId); ++ GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, null); ++ return GlStateManager._getError() != 1285; ++ } ++ + @OnlyIn(Dist.CLIENT) + static enum AttachmentState { + NONE, + COLOR, + DEPTH, +- COLOR_DEPTH; ++ COLOR_DEPTH, ++ STENCIL, ++ COLOR_STENCIL, ++ DEPTH_STENCIL, ++ COLOR_DEPTH_STENCIL; + + private static final MainTarget.AttachmentState[] VALUES = values(); + + MainTarget.AttachmentState with(MainTarget.AttachmentState p_166164_) { + return VALUES[this.ordinal() | p_166164_.ordinal()]; ++ } ++ ++ static MainTarget.AttachmentState of(boolean color, boolean depth, boolean stencil) { ++ var result = NONE; ++ if (color) { ++ result = result.with(COLOR); ++ } ++ if (depth) { ++ result = result.with(DEPTH); ++ } ++ if (stencil) { ++ result = result.with(STENCIL); ++ } ++ ++ return result; + } + } + From 721d79d543de84dd3278f1c79f5f785798a05374 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Mon, 9 Dec 2024 09:33:56 +0000 Subject: [PATCH 07/35] Remove no-op method for enabling depth, pass desired width/height --- .../blaze3d/pipeline/MainTarget.java.patch | 10 ++++--- .../neoforge/client/ClientHooks.java | 4 +-- .../event/ConfigureMainRenderTargetEvent.java | 30 ++++++++++++++----- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 7e4a944801e..4bc19bd1476 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -1,18 +1,20 @@ --- a/com/mojang/blaze3d/pipeline/MainTarget.java +++ b/com/mojang/blaze3d/pipeline/MainTarget.java -@@ -16,7 +_,11 @@ +@@ -16,8 +_,12 @@ static final MainTarget.Dimension DEFAULT_DIMENSIONS = new MainTarget.Dimension(854, 480); public MainTarget(int p_166137_, int p_166138_) { - super(true); -+ this(net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(), p_166137_, p_166138_); +- this.createFrameBuffer(p_166137_, p_166138_); ++ this(net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(true, p_166137_, p_166138_)); + } + -+ private MainTarget(net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent e, int p_166137_, int p_166138_) { ++ private MainTarget(net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent e) { + super(e.useDepth(), e.useStencil()); - this.createFrameBuffer(p_166137_, p_166138_); ++ this.createFrameBuffer(e.width(), e.height()); } + private void createFrameBuffer(int p_166142_, int p_166143_) { @@ -30,13 +_,24 @@ GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index bd56d1fe9e8..f22082411fe 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -1101,8 +1101,8 @@ public static FrameGraphSetupEvent fireFrameGraphSetup(FrameGraphBuilder builder return NeoForge.EVENT_BUS.post(new FrameGraphSetupEvent(builder, targets, renderTargetDescriptor, frustum, camera, modelViewMatrix, projectionMatrix, deltaTracker, profiler)); } - public static ConfigureMainRenderTargetEvent configureMainRenderTarget() { - var e = new ConfigureMainRenderTargetEvent(); + public static ConfigureMainRenderTargetEvent configureMainRenderTarget(boolean useDepth, int width, int height) { + var e = new ConfigureMainRenderTargetEvent(useDepth, width, height); ModLoader.postEvent(e); return e; } diff --git a/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java index c4bc57ad89e..805092c02e4 100644 --- a/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java +++ b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java @@ -23,13 +23,19 @@ * This event is fired on the mod-speciffic event bus, only on the {@linkplain LogicalSide#CLIENT logical client}. */ public class ConfigureMainRenderTargetEvent extends Event implements IModBusEvent { - private boolean useDepth; + private final boolean useDepth; private boolean useStencil; + private final int width; + private final int height; + @ApiStatus.Internal - public ConfigureMainRenderTargetEvent() { - this.useDepth = true; + public ConfigureMainRenderTargetEvent(boolean useDepth, int width, int height) { + this.useDepth = useDepth; this.useStencil = false; + + this.width = width; + this.height = height; } /** @@ -51,13 +57,21 @@ public boolean useStencil() { } /** - * Enable the depth buffer for the main render target. + * Returns the preferred width of the framebuffer. * - * @return this, for method chaining. + * @return The width, in pixels, to attempt to use for the framebuffer. */ - public ConfigureMainRenderTargetEvent enableDepth() { - this.useDepth = true; - return this; + public int width() { + return this.width; + } + + /** + * Returns the preferred height of the framebuffer. + * + * @return The height, in pixels, to attempt to use for the framebuffer. + */ + public int height() { + return this.height; } /** From ff68a10039645ee171bac65137d34d00c1e3a2e2 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Mon, 9 Dec 2024 10:02:16 +0000 Subject: [PATCH 08/35] Minimise patch by removing unnecessary indentation --- .../blaze3d/pipeline/MainTarget.java.patch | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 4bc19bd1476..112e72c6e70 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -15,25 +15,18 @@ } private void createFrameBuffer(int p_166142_, int p_166143_) { -@@ -30,13 +_,24 @@ +@@ -30,6 +_,7 @@ GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); GlStateManager._glFramebufferTexture2D(36160, 36064, 3553, this.colorTextureId, 0); -- GlStateManager._bindTexture(this.depthBufferId); -- GlStateManager._texParameter(3553, 34892, 0); -- GlStateManager._texParameter(3553, 10241, 9728); -- GlStateManager._texParameter(3553, 10240, 9728); -- GlStateManager._texParameter(3553, 10242, 33071); -- GlStateManager._texParameter(3553, 10243, 33071); -- GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); + if (this.useDepth) { -+ GlStateManager._bindTexture(this.depthBufferId); -+ GlStateManager._texParameter(3553, 34892, 0); -+ GlStateManager._texParameter(3553, 10241, 9728); -+ GlStateManager._texParameter(3553, 10240, 9728); -+ GlStateManager._texParameter(3553, 10242, 33071); -+ GlStateManager._texParameter(3553, 10243, 33071); -+ GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); + GlStateManager._bindTexture(this.depthBufferId); + GlStateManager._texParameter(3553, 34892, 0); + GlStateManager._texParameter(3553, 10241, 9728); +@@ -37,6 +_,16 @@ + GlStateManager._texParameter(3553, 10242, 33071); + GlStateManager._texParameter(3553, 10243, 33071); + GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); + } + if (this.useStencil) { + GlStateManager._bindTexture(this.stencilBufferId); @@ -51,9 +44,8 @@ private MainTarget.Dimension allocateAttachments(int p_166147_, int p_166148_) { RenderSystem.assertOnRenderThreadOrInit(); this.colorTextureId = TextureUtil.generateTextureId(); -- this.depthBufferId = TextureUtil.generateTextureId(); + if (this.useDepth) { -+ this.depthBufferId = TextureUtil.generateTextureId(); + this.depthBufferId = TextureUtil.generateTextureId(); + } + if (this.useStencil) { + this.stencilBufferId = this.useDepth ? this.depthBufferId : TextureUtil.generateTextureId(); From 117c7afddc4722d930dff220d90eb75ac5dcd34f Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Mon, 30 Dec 2024 04:55:30 +0000 Subject: [PATCH 09/35] Respond to XFact's feedback --- .../blaze3d/framegraph/FrameGraphBuilder.java.patch | 9 ++++++--- .../net/minecraft/client/renderer/PostChain.java.patch | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch b/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch index b03f4d9e054..84d9e314b44 100644 --- a/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch +++ b/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch @@ -35,12 +35,15 @@ } @OnlyIn(Dist.CLIENT) -@@ -364,5 +_,8 @@ +@@ -363,6 +_,11 @@ + @Override public String toString() { return this.name; - } ++ } + + @Nullable -+ public abstract ResourceDescriptor getDescriptor(); ++ public ResourceDescriptor getDescriptor() { ++ return null; + } } } diff --git a/patches/net/minecraft/client/renderer/PostChain.java.patch b/patches/net/minecraft/client/renderer/PostChain.java.patch index e0c65d1b7ed..0cabd6b5ec2 100644 --- a/patches/net/minecraft/client/renderer/PostChain.java.patch +++ b/patches/net/minecraft/client/renderer/PostChain.java.patch @@ -11,7 +11,7 @@ continue; default: throw new MatchException(null, null); -@@ -95,17 +_,32 @@ +@@ -95,17 +_,33 @@ Matrix4f matrix4f = new Matrix4f().setOrtho(0.0F, (float)p_361423_, 0.0F, (float)p_362735_, 0.1F, 1000.0F); Map> map = new HashMap<>(this.internalTargets.size() + this.externalTargets.size()); @@ -29,8 +29,9 @@ + useDepth |= renderDescriptor.useDepth(); + useStencil |= renderDescriptor.useStencil(); + } else { -+ useDepth |= p_361871_.get(resourcelocation).get().useDepth; -+ useStencil |= p_361871_.get(resourcelocation).get().useStencil; ++ var target = handle.get(); ++ useDepth |= target.useDepth; ++ useStencil |= target.useStencil; + } } From f278e8dae31efcf26d758fc1d8ce8144702e141b Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Thu, 9 Jan 2025 13:30:48 +0000 Subject: [PATCH 10/35] Remove stencil-only patches in MainTarget Other patches are still necessary as other targets have that as a valid combination in vanilla anyway, and post chains can be configured to render to a target with depth disabled. --- .../blaze3d/pipeline/MainTarget.java.patch | 68 +++---------------- 1 file changed, 11 insertions(+), 57 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 112e72c6e70..eb0f6518725 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -15,80 +15,54 @@ } private void createFrameBuffer(int p_166142_, int p_166143_) { -@@ -30,6 +_,7 @@ - GlStateManager._texParameter(3553, 10242, 33071); - GlStateManager._texParameter(3553, 10243, 33071); - GlStateManager._glFramebufferTexture2D(36160, 36064, 3553, this.colorTextureId, 0); -+ if (this.useDepth) { - GlStateManager._bindTexture(this.depthBufferId); - GlStateManager._texParameter(3553, 34892, 0); - GlStateManager._texParameter(3553, 10241, 9728); -@@ -37,6 +_,16 @@ +@@ -37,6 +_,10 @@ GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); -+ } + if (this.useStencil) { + GlStateManager._bindTexture(this.stencilBufferId); -+ GlStateManager._texParameter(3553, 34892, 0); -+ GlStateManager._texParameter(3553, 10241, 9728); -+ GlStateManager._texParameter(3553, 10240, 9728); -+ GlStateManager._texParameter(3553, 10242, 33071); -+ GlStateManager._texParameter(3553, 10243, 33071); + GlStateManager._glFramebufferTexture2D(36160, org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, 3553, this.stencilBufferId, 0); + } GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; -@@ -49,8 +_,14 @@ - private MainTarget.Dimension allocateAttachments(int p_166147_, int p_166148_) { +@@ -50,7 +_,14 @@ RenderSystem.assertOnRenderThreadOrInit(); this.colorTextureId = TextureUtil.generateTextureId(); -+ if (this.useDepth) { this.depthBufferId = TextureUtil.generateTextureId(); -+ } + if (this.useStencil) { -+ this.stencilBufferId = this.useDepth ? this.depthBufferId : TextureUtil.generateTextureId(); ++ this.stencilBufferId = this.depthBufferId; + } MainTarget.AttachmentState maintarget$attachmentstate = MainTarget.AttachmentState.NONE; -+ MainTarget.AttachmentState targetState = MainTarget.AttachmentState.of(true, this.useDepth, this.useStencil); ++ MainTarget.AttachmentState targetState = MainTarget.AttachmentState.COLOR_DEPTH; ++ if (this.useStencil) { ++ targetState = targetState.with(MainTarget.AttachmentState.STENCIL); ++ } for (MainTarget.Dimension maintarget$dimension : MainTarget.Dimension.listWithFallback(p_166147_, p_166148_)) { maintarget$attachmentstate = MainTarget.AttachmentState.NONE; -@@ -58,11 +_,19 @@ +@@ -58,11 +_,15 @@ maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.COLOR); } - if (this.allocateDepthAttachment(maintarget$dimension)) { -+ if (this.useDepth && this.useStencil && this.allocateDepthStencilAttachment(maintarget$dimension)) { ++ if (this.useStencil && this.allocateDepthStencilAttachment(maintarget$dimension)) { + maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH_STENCIL); + } + -+ else if (this.useDepth && this.allocateDepthAttachment(maintarget$dimension)) { ++ else if (this.allocateDepthAttachment(maintarget$dimension)) { maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH); } - if (maintarget$attachmentstate == MainTarget.AttachmentState.COLOR_DEPTH) { -+ else if (this.useStencil && this.allocateStencilAttachment(maintarget$dimension)) { -+ maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.STENCIL); -+ } -+ + if (maintarget$attachmentstate == targetState) { return maintarget$dimension; } } -@@ -86,17 +_,52 @@ +@@ -86,12 +_,24 @@ return GlStateManager._getError() != 1285; } -+ private boolean allocateStencilAttachment(MainTarget.Dimension p_166145_) { -+ RenderSystem.assertOnRenderThreadOrInit(); -+ GlStateManager._getError(); -+ GlStateManager._bindTexture(this.stencilBufferId); -+ GlStateManager._texImage2D(3553, 0, org.lwjgl.opengl.GL32.GL_STENCIL_INDEX8, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_STENCIL_INDEX, org.lwjgl.opengl.GL32.GL_BYTE, null); -+ return GlStateManager._getError() != 1285; -+ } -+ + private boolean allocateDepthStencilAttachment(MainTarget.Dimension p_166145_) { + RenderSystem.assertOnRenderThreadOrInit(); + GlStateManager._getError(); @@ -111,23 +85,3 @@ private static final MainTarget.AttachmentState[] VALUES = values(); - MainTarget.AttachmentState with(MainTarget.AttachmentState p_166164_) { - return VALUES[this.ordinal() | p_166164_.ordinal()]; -+ } -+ -+ static MainTarget.AttachmentState of(boolean color, boolean depth, boolean stencil) { -+ var result = NONE; -+ if (color) { -+ result = result.with(COLOR); -+ } -+ if (depth) { -+ result = result.with(DEPTH); -+ } -+ if (stencil) { -+ result = result.with(STENCIL); -+ } -+ -+ return result; - } - } - From 552d6360ff7ce532a7dceb36134f0055ae069917 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:14:08 +0100 Subject: [PATCH 11/35] Stencil requires depth --- .../blaze3d/pipeline/MainTarget.java.patch | 11 +-- .../blaze3d/pipeline/RenderTarget.java.patch | 89 +++++-------------- 2 files changed, 23 insertions(+), 77 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index eb0f6518725..9a87eaa883f 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -15,24 +15,19 @@ } private void createFrameBuffer(int p_166142_, int p_166143_) { -@@ -37,6 +_,10 @@ +@@ -37,6 +_,9 @@ GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); + if (this.useStencil) { -+ GlStateManager._bindTexture(this.stencilBufferId); -+ GlStateManager._glFramebufferTexture2D(36160, org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, 3553, this.stencilBufferId, 0); ++ GlStateManager._glFramebufferTexture2D(36160, org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, 3553, this.depthBufferId, 0); + } GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; -@@ -50,7 +_,14 @@ - RenderSystem.assertOnRenderThreadOrInit(); +@@ -51,6 +_,10 @@ this.colorTextureId = TextureUtil.generateTextureId(); this.depthBufferId = TextureUtil.generateTextureId(); -+ if (this.useStencil) { -+ this.stencilBufferId = this.depthBufferId; -+ } MainTarget.AttachmentState maintarget$attachmentstate = MainTarget.AttachmentState.NONE; + MainTarget.AttachmentState targetState = MainTarget.AttachmentState.COLOR_DEPTH; + if (this.useStencil) { diff --git a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch index bca255906d1..11876e73ad7 100644 --- a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch @@ -1,6 +1,6 @@ --- a/com/mojang/blaze3d/pipeline/RenderTarget.java +++ b/com/mojang/blaze3d/pipeline/RenderTarget.java -@@ -25,17 +_,31 @@ +@@ -25,6 +_,7 @@ public int viewWidth; public int viewHeight; public final boolean useDepth; @@ -8,8 +8,7 @@ public int frameBufferId; protected int colorTextureId; protected int depthBufferId; -+ protected int stencilBufferId; - private final float[] clearChannels = Util.make(() -> new float[]{1.0F, 1.0F, 1.0F, 0.0F}); +@@ -32,7 +_,15 @@ public int filterMode; public RenderTarget(boolean p_166199_) { @@ -18,84 +17,36 @@ + } + + public RenderTarget(boolean useDepth, boolean useStencil) { ++ if (useStencil && !useDepth) { ++ throw new IllegalArgumentException("Stencil can only be enabled if depth is enabled."); ++ } + this.useDepth = useDepth; + this.useStencil = useStencil; this.frameBufferId = -1; this.colorTextureId = -1; this.depthBufferId = -1; -+ -+ if (!useDepth && useStencil) { -+ var capabilities = org.lwjgl.opengl.GL.getCapabilities(); -+ if (!capabilities.GL_ARB_texture_stencil8 && !capabilities.OpenGL44) { -+ throw new UnsupportedOperationException("Stencil-only buffers require GL_ARB_texture_stencil8 OR OpenGL 4.4"); -+ } -+ } - } - - public void resize(int p_83942_, int p_83943_) { -@@ -53,6 +_,11 @@ - RenderSystem.assertOnRenderThreadOrInit(); - this.unbindRead(); - this.unbindWrite(); -+ if (this.stencilBufferId > -1 && this.stencilBufferId != this.depthBufferId) { -+ TextureUtil.releaseTextureId(this.stencilBufferId); -+ this.stencilBufferId = -1; -+ } -+ - if (this.depthBufferId > -1) { - TextureUtil.releaseTextureId(this.depthBufferId); - this.depthBufferId = -1; -@@ -96,9 +_,50 @@ +@@ -96,7 +_,20 @@ GlStateManager._texParameter(3553, 34892, 0); GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); -+ if (!this.useStencil) // If stenciling is enabled, we will fill this later - GlStateManager._texImage2D(3553, 0, 6402, this.width, this.height, 0, 6402, 5126, null); - } - -+ if (this.useStencil) { -+ if (this.useDepth) { -+ // If depth and stencil buffers are both enabled, we must combine them -+ this.stencilBufferId = this.depthBufferId; +- GlStateManager._texImage2D(3553, 0, 6402, this.width, this.height, 0, 6402, 5126, null); ++ if (!this.useStencil) { ++ GlStateManager._texImage2D(3553, 0, 6402, this.width, this.height, 0, 6402, 5126, null); + } else { -+ // Otherwise, we can generate a new texture in its place. -+ this.stencilBufferId = TextureUtil.generateTextureId(); -+ GlStateManager._bindTexture(this.stencilBufferId); -+ GlStateManager._texParameter(3553, 10241, 9728); -+ GlStateManager._texParameter(3553, 10240, 9728); -+ GlStateManager._texParameter(3553, 34892, 0); -+ GlStateManager._texParameter(3553, 10242, 33071); -+ GlStateManager._texParameter(3553, 10243, 33071); -+ } -+ -+ if (this.useDepth) { + // Use a combined format for both depth and stencil. + GlStateManager._texImage2D( -+ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, -+ 0, -+ org.lwjgl.opengl.GL32.GL_DEPTH24_STENCIL8, -+ this.width, this.height, -+ 0, -+ org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, -+ org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, -+ null); -+ } else { -+ // Otherwise, we can use a separate format. Testing for this was done in the constructor already. -+ GlStateManager._texImage2D( -+ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, -+ 0, -+ org.lwjgl.opengl.GL32.GL_STENCIL_INDEX8, -+ this.width, this.height, -+ 0, -+ org.lwjgl.opengl.GL32.GL_STENCIL_INDEX, -+ org.lwjgl.opengl.GL32.GL_BYTE, -+ null); ++ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, ++ 0, ++ org.lwjgl.opengl.GL32.GL_DEPTH24_STENCIL8, ++ this.width, this.height, ++ 0, ++ org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, ++ org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, ++ null); + } -+ } -+ + } + this.setFilterMode(9728, true); - GlStateManager._bindTexture(this.colorTextureId); - GlStateManager._texParameter(3553, 10242, 33071); @@ -109,6 +_,14 @@ if (this.useDepth) { GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); @@ -105,7 +56,7 @@ + org.lwjgl.opengl.GL32.GL_FRAMEBUFFER, + org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, + org.lwjgl.opengl.GL32.GL_TEXTURE_2D, -+ this.stencilBufferId, ++ this.depthBufferId, + 0); + } From 2278de18d0488a30fd971a054c21675a944d7e54 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:20:47 +0100 Subject: [PATCH 12/35] Remove unnecessary AttachmentState patch --- .../blaze3d/pipeline/MainTarget.java.patch | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 9a87eaa883f..9df3ce3d3a7 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -25,58 +25,31 @@ GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; -@@ -51,6 +_,10 @@ - this.colorTextureId = TextureUtil.generateTextureId(); - this.depthBufferId = TextureUtil.generateTextureId(); - MainTarget.AttachmentState maintarget$attachmentstate = MainTarget.AttachmentState.NONE; -+ MainTarget.AttachmentState targetState = MainTarget.AttachmentState.COLOR_DEPTH; -+ if (this.useStencil) { -+ targetState = targetState.with(MainTarget.AttachmentState.STENCIL); -+ } - - for (MainTarget.Dimension maintarget$dimension : MainTarget.Dimension.listWithFallback(p_166147_, p_166148_)) { - maintarget$attachmentstate = MainTarget.AttachmentState.NONE; -@@ -58,11 +_,15 @@ +@@ -58,7 +_,11 @@ maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.COLOR); } - if (this.allocateDepthAttachment(maintarget$dimension)) { + if (this.useStencil && this.allocateDepthStencilAttachment(maintarget$dimension)) { -+ maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH_STENCIL); ++ maintarget$attachmentstate = maintarget$attachmentstate.with(AttachmentState.DEPTH); + } + + else if (this.allocateDepthAttachment(maintarget$dimension)) { maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH); } -- if (maintarget$attachmentstate == MainTarget.AttachmentState.COLOR_DEPTH) { -+ if (maintarget$attachmentstate == targetState) { - return maintarget$dimension; - } - } -@@ -86,12 +_,24 @@ - return GlStateManager._getError() != 1285; - } - +@@ -83,6 +_,14 @@ + GlStateManager._getError(); + GlStateManager._bindTexture(this.depthBufferId); + GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); ++ return GlStateManager._getError() != 1285; ++ } ++ + private boolean allocateDepthStencilAttachment(MainTarget.Dimension p_166145_) { + RenderSystem.assertOnRenderThreadOrInit(); + GlStateManager._getError(); + GlStateManager._bindTexture(this.depthBufferId); + GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, null); -+ return GlStateManager._getError() != 1285; -+ } -+ - @OnlyIn(Dist.CLIENT) - static enum AttachmentState { - NONE, - COLOR, - DEPTH, -- COLOR_DEPTH; -+ COLOR_DEPTH, -+ STENCIL, -+ COLOR_STENCIL, -+ DEPTH_STENCIL, -+ COLOR_DEPTH_STENCIL; - - private static final MainTarget.AttachmentState[] VALUES = values(); + return GlStateManager._getError() != 1285; + } From fa4ebe71660dbb0e8c10738bdc5e7595b6f3d274 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:27:43 +0100 Subject: [PATCH 13/35] Keep simplifying --- .../blaze3d/pipeline/RenderTarget.java.patch | 18 ++++-------------- .../client/renderer/PostPass.java.patch | 10 ++-------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch index 11876e73ad7..4d6619c9184 100644 --- a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch @@ -62,24 +62,14 @@ this.checkStatus(); this.clear(); -@@ -218,6 +_,10 @@ +@@ -217,6 +_,10 @@ + if (this.useDepth) { GlStateManager._clearDepth(1.0); i |= 256; - } ++ } + if (this.useStencil) { + GlStateManager._clearStencil(0); + i |= org.lwjgl.opengl.GL32.GL_STENCIL_BUFFER_BIT; -+ } + } GlStateManager._clear(i); - this.unbindWrite(); -@@ -229,5 +_,9 @@ - - public int getDepthTextureId() { - return this.depthBufferId; -+ } -+ -+ public int getStencilTextureId() { -+ return this.stencilBufferId; - } - } diff --git a/patches/net/minecraft/client/renderer/PostPass.java.patch b/patches/net/minecraft/client/renderer/PostPass.java.patch index efa98f666ba..b5e564963f0 100644 --- a/patches/net/minecraft/client/renderer/PostPass.java.patch +++ b/patches/net/minecraft/client/renderer/PostPass.java.patch @@ -56,18 +56,12 @@ private ResourceHandle getHandle(Map> p_364534_) { ResourceHandle resourcehandle = p_364534_.get(this.targetId); if (resourcehandle == null) { -@@ -142,7 +_,13 @@ +@@ -142,7 +_,7 @@ ResourceHandle resourcehandle = this.getHandle(p_361239_); RenderTarget rendertarget = resourcehandle.get(); rendertarget.setFilterMode(this.bilinear ? 9729 : 9728); - p_366564_.bindSampler(this.samplerName + "Sampler", this.depthBuffer ? rendertarget.getDepthTextureId() : rendertarget.getColorTextureId()); -+ if (this.depthBuffer()) -+ p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getDepthTextureId()); -+ // If stencil is specified -+ else if (this.stencilBuffer()) -+ p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getStencilTextureId()); -+ else -+ p_366564_.bindSampler(this.samplerName + "Sampler", rendertarget.getColorTextureId()); ++ p_366564_.bindSampler(this.samplerName + "Sampler", this.depthBuffer() ? rendertarget.getDepthTextureId() : rendertarget.getColorTextureId()); p_366564_.safeGetUniform(this.samplerName + "Size").set((float)rendertarget.width, (float)rendertarget.height); } From f3df3e41dc9a44d5275054aaf22fb3bc99907487 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:45:27 +0100 Subject: [PATCH 14/35] Keep simplifying? --- .../client/renderer/PostChain.java.patch | 11 ---- .../renderer/PostChainConfig.java.patch | 23 -------- .../client/renderer/PostPass.java.patch | 56 ------------------- 3 files changed, 90 deletions(-) delete mode 100644 patches/net/minecraft/client/renderer/PostChainConfig.java.patch diff --git a/patches/net/minecraft/client/renderer/PostChain.java.patch b/patches/net/minecraft/client/renderer/PostChain.java.patch index 0cabd6b5ec2..ad7a8d4e9e3 100644 --- a/patches/net/minecraft/client/renderer/PostChain.java.patch +++ b/patches/net/minecraft/client/renderer/PostChain.java.patch @@ -1,16 +1,5 @@ --- a/net/minecraft/client/renderer/PostChain.java +++ b/net/minecraft/client/renderer/PostChain.java -@@ -79,8 +_,8 @@ - abstracttexture.setFilter(flag, false); - postpass.addInput(new PostPass.TextureInput(s3, abstracttexture, i, j)); - continue; -- case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2): -- postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, flag1, flag2)); -+ case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation1, boolean flag1, boolean flag2, boolean useStencilBuffer): -+ postpass.addInput(new PostPass.TargetInput(s1, resourcelocation1, net.minecraft.client.renderer.PostPass.TargetInput.BufferType.from(flag1, useStencilBuffer), flag2)); - continue; - default: - throw new MatchException(null, null); @@ -95,17 +_,33 @@ Matrix4f matrix4f = new Matrix4f().setOrtho(0.0F, (float)p_361423_, 0.0F, (float)p_362735_, 0.1F, 1000.0F); Map> map = new HashMap<>(this.internalTargets.size() + this.externalTargets.size()); diff --git a/patches/net/minecraft/client/renderer/PostChainConfig.java.patch b/patches/net/minecraft/client/renderer/PostChainConfig.java.patch deleted file mode 100644 index e07b4d10af6..00000000000 --- a/patches/net/minecraft/client/renderer/PostChainConfig.java.patch +++ /dev/null @@ -1,23 +0,0 @@ ---- a/net/minecraft/client/renderer/PostChainConfig.java -+++ b/net/minecraft/client/renderer/PostChainConfig.java -@@ -108,13 +_,18 @@ - } - - @OnlyIn(Dist.CLIENT) -- public static record TargetInput(String samplerName, ResourceLocation targetId, boolean useDepthBuffer, boolean bilinear) implements PostChainConfig.Input { -+ public static record TargetInput(String samplerName, ResourceLocation targetId, boolean useDepthBuffer, boolean bilinear, boolean useStencilBuffer) implements PostChainConfig.Input { -+ public TargetInput(String samplerName, ResourceLocation targetId, boolean useDepthBuffer, boolean bilinear) { -+ this(samplerName, targetId, useDepthBuffer, bilinear, false); -+ } -+ - public static final Codec CODEC = RecordCodecBuilder.create( - p_363892_ -> p_363892_.group( - Codec.STRING.fieldOf("sampler_name").forGetter(PostChainConfig.TargetInput::samplerName), - ResourceLocation.CODEC.fieldOf("target").forGetter(PostChainConfig.TargetInput::targetId), - Codec.BOOL.optionalFieldOf("use_depth_buffer", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::useDepthBuffer), -- Codec.BOOL.optionalFieldOf("bilinear", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::bilinear) -+ Codec.BOOL.optionalFieldOf("bilinear", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::bilinear), -+ Codec.BOOL.optionalFieldOf("neoforge:use_stencil_buffer", Boolean.valueOf(false)).forGetter(PostChainConfig.TargetInput::useStencilBuffer) - ) - .apply(p_363892_, PostChainConfig.TargetInput::new) - ); diff --git a/patches/net/minecraft/client/renderer/PostPass.java.patch b/patches/net/minecraft/client/renderer/PostPass.java.patch index b5e564963f0..38d3c002a9f 100644 --- a/patches/net/minecraft/client/renderer/PostPass.java.patch +++ b/patches/net/minecraft/client/renderer/PostPass.java.patch @@ -1,61 +1,5 @@ --- a/net/minecraft/client/renderer/PostPass.java +++ b/net/minecraft/client/renderer/PostPass.java -@@ -122,7 +_,54 @@ - } - - @OnlyIn(Dist.CLIENT) -- public static record TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear) implements PostPass.Input { -+ public static record TargetInput(String samplerName, ResourceLocation targetId, BufferType bufferType, boolean bilinear) implements PostPass.Input { -+ public enum BufferType { -+ NONE(false, false), -+ DEPTH_ONLY(true, false), -+ STENCIL_ONLY(false, true), -+ DEPTH_STENCIL(true, true); -+ -+ private final boolean depth; -+ private final boolean stencil; -+ -+ BufferType(boolean depth, boolean stencil) { -+ this.depth = depth; -+ this.stencil = stencil; -+ } -+ -+ public boolean hasDepth() { -+ return this.depth; -+ } -+ -+ public boolean hasStencil() { -+ return this.stencil; -+ } -+ -+ public static BufferType from(boolean useDepth, boolean useStencil) { -+ if (useDepth && useStencil) { -+ return DEPTH_STENCIL; -+ } else if (useDepth) { -+ return DEPTH_ONLY; -+ } else if (useStencil) { -+ return STENCIL_ONLY; -+ } else { -+ return NONE; -+ } -+ } -+ } -+ -+ public TargetInput(String samplerName, ResourceLocation targetId, boolean depthBuffer, boolean bilinear) { -+ this(samplerName, targetId, depthBuffer ? BufferType.DEPTH_ONLY : BufferType.NONE, bilinear); -+ } -+ -+ public boolean depthBuffer() { -+ return bufferType.hasDepth(); -+ } -+ -+ public boolean stencilBuffer() { -+ return bufferType.hasStencil(); -+ } -+ - private ResourceHandle getHandle(Map> p_364534_) { - ResourceHandle resourcehandle = p_364534_.get(this.targetId); - if (resourcehandle == null) { @@ -142,7 +_,7 @@ ResourceHandle resourcehandle = this.getHandle(p_361239_); RenderTarget rendertarget = resourcehandle.get(); From 46e68fed2c644a9f6f618282f926d9bc102d44c1 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:50:10 +0100 Subject: [PATCH 15/35] Missed old patch to delete --- .../net/minecraft/client/renderer/PostPass.java.patch | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 patches/net/minecraft/client/renderer/PostPass.java.patch diff --git a/patches/net/minecraft/client/renderer/PostPass.java.patch b/patches/net/minecraft/client/renderer/PostPass.java.patch deleted file mode 100644 index 38d3c002a9f..00000000000 --- a/patches/net/minecraft/client/renderer/PostPass.java.patch +++ /dev/null @@ -1,11 +0,0 @@ ---- a/net/minecraft/client/renderer/PostPass.java -+++ b/net/minecraft/client/renderer/PostPass.java -@@ -142,7 +_,7 @@ - ResourceHandle resourcehandle = this.getHandle(p_361239_); - RenderTarget rendertarget = resourcehandle.get(); - rendertarget.setFilterMode(this.bilinear ? 9729 : 9728); -- p_366564_.bindSampler(this.samplerName + "Sampler", this.depthBuffer ? rendertarget.getDepthTextureId() : rendertarget.getColorTextureId()); -+ p_366564_.bindSampler(this.samplerName + "Sampler", this.depthBuffer() ? rendertarget.getDepthTextureId() : rendertarget.getColorTextureId()); - p_366564_.safeGetUniform(this.samplerName + "Size").set((float)rendertarget.width, (float)rendertarget.height); - } - From 02a1ea2fa0115919ad60d053b52d2ff2b8e6e861 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:53:46 +0100 Subject: [PATCH 16/35] getDescriptor simplification --- .../framegraph/FrameGraphBuilder.java.patch | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch b/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch index 84d9e314b44..d9d632bd4a1 100644 --- a/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch +++ b/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch @@ -1,22 +1,11 @@ --- a/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java +++ b/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java -@@ -173,6 +_,11 @@ - public T get() { - return this.resource; - } -+ -+ @Override -+ public ResourceDescriptor getDescriptor() { -+ return null; -+ } - } - - @OnlyIn(Dist.CLIENT) -@@ -211,6 +_,10 @@ +@@ -211,6 +_,11 @@ public String toString() { return this.createdBy != null ? this.holder + "#" + this.version + " (from " + this.createdBy + ")" : this.holder + "#" + this.version; } + ++ @Nullable + public ResourceDescriptor getDescriptor() { + return this.holder.getDescriptor(); + } From f62e41755b774e1496d70585d35307fb7ae567dd Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Thu, 9 Jan 2025 20:50:11 +0100 Subject: [PATCH 17/35] Simplified event --- .../blaze3d/pipeline/MainTarget.java.patch | 8 +-- .../net/minecraft/client/Minecraft.java.patch | 3 +- .../neoforge/client/ClientHooks.java | 9 ++-- .../event/ConfigureMainRenderTargetEvent.java | 50 ++----------------- 4 files changed, 14 insertions(+), 56 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 9df3ce3d3a7..f6127abcc72 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -6,12 +6,12 @@ public MainTarget(int p_166137_, int p_166138_) { - super(true); - this.createFrameBuffer(p_166137_, p_166138_); -+ this(net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(true, p_166137_, p_166138_)); ++ this(p_166137_, p_166138_, false); + } + -+ private MainTarget(net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent e) { -+ super(e.useDepth(), e.useStencil()); -+ this.createFrameBuffer(e.width(), e.height()); ++ public MainTarget(int width, int height, boolean useStencil) { ++ super(true, useStencil); ++ this.createFrameBuffer(width, height); } private void createFrameBuffer(int p_166142_, int p_166143_) { diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 4b3dc587ed0..c59651003d2 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -18,8 +18,9 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); +- this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); + net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); - this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); ++ this.mainRenderTarget = net.neoforged.neoforge.client.ClientHooks.createMainRenderTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); this.mainRenderTarget.clear(); this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index f22082411fe..11d1307d91a 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -9,6 +9,7 @@ import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableMap; import com.mojang.blaze3d.framegraph.FrameGraphBuilder; +import com.mojang.blaze3d.pipeline.MainTarget; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.Window; import com.mojang.blaze3d.resource.RenderTargetDescriptor; @@ -1101,9 +1102,9 @@ public static FrameGraphSetupEvent fireFrameGraphSetup(FrameGraphBuilder builder return NeoForge.EVENT_BUS.post(new FrameGraphSetupEvent(builder, targets, renderTargetDescriptor, frustum, camera, modelViewMatrix, projectionMatrix, deltaTracker, profiler)); } - public static ConfigureMainRenderTargetEvent configureMainRenderTarget(boolean useDepth, int width, int height) { - var e = new ConfigureMainRenderTargetEvent(useDepth, width, height); - ModLoader.postEvent(e); - return e; + @ApiStatus.Internal + public static MainTarget createMainRenderTarget(int width, int height) { + var e = ModLoader.postEventWithReturn(new ConfigureMainRenderTargetEvent()); + return new MainTarget(width, height, e.useStencil()); } } diff --git a/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java index 805092c02e4..92b13184584 100644 --- a/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java +++ b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java @@ -6,49 +6,23 @@ package net.neoforged.neoforge.client.event; import com.mojang.blaze3d.pipeline.MainTarget; -import com.mojang.blaze3d.pipeline.RenderTarget; import net.neoforged.bus.api.Event; import net.neoforged.bus.api.ICancellableEvent; import net.neoforged.fml.LogicalSide; import net.neoforged.fml.event.IModBusEvent; -import org.jetbrains.annotations.ApiStatus; /** - * Fired when configuring the main {@linkplain RenderTarget render target}. - *

- * This event fires during startup when the {@link MainTarget} is constructed. + * Fired when configuring the {@linkplain MainTarget main render target} during startup. *

* This event is not {@linkplain ICancellableEvent cancellable}. *

- * This event is fired on the mod-speciffic event bus, only on the {@linkplain LogicalSide#CLIENT logical client}. + * This event is fired on the mod-specific event bus, only on the {@linkplain LogicalSide#CLIENT logical client}. */ public class ConfigureMainRenderTargetEvent extends Event implements IModBusEvent { - private final boolean useDepth; private boolean useStencil; - private final int width; - private final int height; - - @ApiStatus.Internal - public ConfigureMainRenderTargetEvent(boolean useDepth, int width, int height) { - this.useDepth = useDepth; - this.useStencil = false; - - this.width = width; - this.height = height; - } - - /** - * Returns whether the depth buffer is enabled. - * - * @return true, if the depth buffer is enabled, or false otherwise. - */ - public boolean useDepth() { - return this.useDepth; - } - /** - * Returns whether the stencil buffer is enabled. + * Returns whether enabling the stencil buffer on the main render target was requested. * * @return true, if the stencil buffer is enabled, or false otherwise. */ @@ -56,24 +30,6 @@ public boolean useStencil() { return this.useStencil; } - /** - * Returns the preferred width of the framebuffer. - * - * @return The width, in pixels, to attempt to use for the framebuffer. - */ - public int width() { - return this.width; - } - - /** - * Returns the preferred height of the framebuffer. - * - * @return The height, in pixels, to attempt to use for the framebuffer. - */ - public int height() { - return this.height; - } - /** * Enable the stencil buffer for the main render target. * From 3f5ddc10700f9f022130ef6b96f39f272d6a6749 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 9 Jan 2025 23:02:43 +0100 Subject: [PATCH 18/35] Simplify allocation (+ missed genPatches) --- .../blaze3d/pipeline/MainTarget.java.patch | 36 +++++++++---------- .../net/minecraft/client/Minecraft.java.patch | 3 +- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index f6127abcc72..53624be3982 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -6,12 +6,12 @@ public MainTarget(int p_166137_, int p_166138_) { - super(true); - this.createFrameBuffer(p_166137_, p_166138_); -+ this(p_166137_, p_166138_, false); ++ this(net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(true, p_166137_, p_166138_)); + } + -+ public MainTarget(int width, int height, boolean useStencil) { -+ super(true, useStencil); -+ this.createFrameBuffer(width, height); ++ private MainTarget(net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent e) { ++ super(e.useDepth(), e.useStencil()); ++ this.createFrameBuffer(e.width(), e.height()); } private void createFrameBuffer(int p_166142_, int p_166143_) { @@ -25,31 +25,27 @@ GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; -@@ -58,7 +_,11 @@ +@@ -58,8 +_,8 @@ maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.COLOR); } - if (this.allocateDepthAttachment(maintarget$dimension)) { -+ if (this.useStencil && this.allocateDepthStencilAttachment(maintarget$dimension)) { +- maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH); ++ if (this.useStencil && this.allocateDepthAttachment(maintarget$dimension)) { + maintarget$attachmentstate = maintarget$attachmentstate.with(AttachmentState.DEPTH); -+ } -+ -+ else if (this.allocateDepthAttachment(maintarget$dimension)) { - maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH); } -@@ -83,6 +_,14 @@ + if (maintarget$attachmentstate == MainTarget.AttachmentState.COLOR_DEPTH) { +@@ -82,7 +_,11 @@ + RenderSystem.assertOnRenderThreadOrInit(); GlStateManager._getError(); GlStateManager._bindTexture(this.depthBufferId); - GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); -+ return GlStateManager._getError() != 1285; -+ } -+ -+ private boolean allocateDepthStencilAttachment(MainTarget.Dimension p_166145_) { -+ RenderSystem.assertOnRenderThreadOrInit(); -+ GlStateManager._getError(); -+ GlStateManager._bindTexture(this.depthBufferId); -+ GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, null); +- GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); ++ if (this.useStencil) { ++ GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, null); ++ } else { ++ GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); ++ } return GlStateManager._getError() != 1285; } diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index c59651003d2..4b3dc587ed0 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -18,9 +18,8 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); -- this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); + net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); -+ this.mainRenderTarget = net.neoforged.neoforge.client.ClientHooks.createMainRenderTarget(this.window.getWidth(), this.window.getHeight()); + this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); this.mainRenderTarget.clear(); this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); From 1f62c68db2a83e124dea2825218f871fc07cd485 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Tue, 14 Jan 2025 18:47:13 +0100 Subject: [PATCH 19/35] Fix patches that I didn't cherry pick --- patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch | 8 ++++---- patches/net/minecraft/client/Minecraft.java.patch | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 53624be3982..cc1ede04515 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -6,12 +6,12 @@ public MainTarget(int p_166137_, int p_166138_) { - super(true); - this.createFrameBuffer(p_166137_, p_166138_); -+ this(net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(true, p_166137_, p_166138_)); ++ this(p_166137_, p_166138_, false); + } + -+ private MainTarget(net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent e) { -+ super(e.useDepth(), e.useStencil()); -+ this.createFrameBuffer(e.width(), e.height()); ++ public MainTarget(int width, int height, boolean useStencil) { ++ super(true, useStencil); ++ this.createFrameBuffer(width, height); } private void createFrameBuffer(int p_166142_, int p_166143_) { diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 4b3dc587ed0..c59651003d2 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -18,8 +18,9 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); +- this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); + net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); - this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); ++ this.mainRenderTarget = net.neoforged.neoforge.client.ClientHooks.createMainRenderTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); this.mainRenderTarget.clear(); this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); From 3a9cf29384363db3f67e9530f5335b65804e2b56 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Tue, 14 Jan 2025 18:49:54 +0100 Subject: [PATCH 20/35] Remove bad patch (my bad) --- .../com/mojang/blaze3d/pipeline/MainTarget.java.patch | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index cc1ede04515..cf2e8a23b61 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -25,17 +25,6 @@ GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; -@@ -58,8 +_,8 @@ - maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.COLOR); - } - -- if (this.allocateDepthAttachment(maintarget$dimension)) { -- maintarget$attachmentstate = maintarget$attachmentstate.with(MainTarget.AttachmentState.DEPTH); -+ if (this.useStencil && this.allocateDepthAttachment(maintarget$dimension)) { -+ maintarget$attachmentstate = maintarget$attachmentstate.with(AttachmentState.DEPTH); - } - - if (maintarget$attachmentstate == MainTarget.AttachmentState.COLOR_DEPTH) { @@ -82,7 +_,11 @@ RenderSystem.assertOnRenderThreadOrInit(); GlStateManager._getError(); From d4e04841113c424c2961520e76a7405cb15649fb Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:00:05 +0100 Subject: [PATCH 21/35] Experiment with going back to a global stencil parameter, still configured by an event --- .../framegraph/FrameGraphBuilder.java.patch | 38 ------------------- .../blaze3d/pipeline/MainTarget.java.patch | 12 ++---- .../net/minecraft/client/Minecraft.java.patch | 6 +-- .../client/renderer/PostChain.java.patch | 30 ++------------- .../neoforge/client/ClientHooks.java | 18 +++++++-- 5 files changed, 24 insertions(+), 80 deletions(-) delete mode 100644 patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch diff --git a/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch b/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch deleted file mode 100644 index d9d632bd4a1..00000000000 --- a/patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch +++ /dev/null @@ -1,38 +0,0 @@ ---- a/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java -+++ b/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java -@@ -211,6 +_,11 @@ - public String toString() { - return this.createdBy != null ? this.holder + "#" + this.version + " (from " + this.createdBy + ")" : this.holder + "#" + this.version; - } -+ -+ @Nullable -+ public ResourceDescriptor getDescriptor() { -+ return this.holder.getDescriptor(); -+ } - } - - @OnlyIn(Dist.CLIENT) -@@ -265,6 +_,11 @@ - this.physicalResource = null; - } - } -+ -+ @Override -+ public ResourceDescriptor getDescriptor() { -+ return descriptor; -+ } - } - - @OnlyIn(Dist.CLIENT) -@@ -363,6 +_,11 @@ - @Override - public String toString() { - return this.name; -+ } -+ -+ @Nullable -+ public ResourceDescriptor getDescriptor() { -+ return null; - } - } - } diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index cf2e8a23b61..36aa1264950 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -1,20 +1,14 @@ --- a/com/mojang/blaze3d/pipeline/MainTarget.java +++ b/com/mojang/blaze3d/pipeline/MainTarget.java -@@ -16,8 +_,12 @@ +@@ -16,7 +_,7 @@ static final MainTarget.Dimension DEFAULT_DIMENSIONS = new MainTarget.Dimension(854, 480); public MainTarget(int p_166137_, int p_166138_) { - super(true); -- this.createFrameBuffer(p_166137_, p_166138_); -+ this(p_166137_, p_166138_, false); -+ } -+ -+ public MainTarget(int width, int height, boolean useStencil) { -+ super(true, useStencil); -+ this.createFrameBuffer(width, height); ++ super(true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); + this.createFrameBuffer(p_166137_, p_166138_); } - private void createFrameBuffer(int p_166142_, int p_166143_) { @@ -37,6 +_,9 @@ GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index c59651003d2..30d5573d241 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -8,7 +8,7 @@ this.demo = p_91084_.game.demo; this.allowsMultiplayer = !p_91084_.game.disableMultiplayer; this.allowsChat = !p_91084_.game.disableChat; -@@ -483,15 +_,18 @@ +@@ -483,15 +_,19 @@ LOGGER.error("Couldn't set icon", (Throwable)ioexception); } @@ -18,9 +18,9 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); -- this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); + net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); -+ this.mainRenderTarget = net.neoforged.neoforge.client.ClientHooks.createMainRenderTarget(this.window.getWidth(), this.window.getHeight()); ++ net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(); + this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); this.mainRenderTarget.clear(); this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); diff --git a/patches/net/minecraft/client/renderer/PostChain.java.patch b/patches/net/minecraft/client/renderer/PostChain.java.patch index ad7a8d4e9e3..adeeadd6147 100644 --- a/patches/net/minecraft/client/renderer/PostChain.java.patch +++ b/patches/net/minecraft/client/renderer/PostChain.java.patch @@ -1,38 +1,14 @@ --- a/net/minecraft/client/renderer/PostChain.java +++ b/net/minecraft/client/renderer/PostChain.java -@@ -95,17 +_,33 @@ - Matrix4f matrix4f = new Matrix4f().setOrtho(0.0F, (float)p_361423_, 0.0F, (float)p_362735_, 0.1F, 1000.0F); - Map> map = new HashMap<>(this.internalTargets.size() + this.externalTargets.size()); - -+ // Enable the depth and stencil buffers based on whether any external targets use them. -+ // This is necessary so any created buffers get the correct parameters for blitting. -+ boolean useDepth = false; -+ boolean useStencil = false; - for (ResourceLocation resourcelocation : this.externalTargets) { - map.put(resourcelocation, p_361871_.getOrThrow(resourcelocation)); -+ -+ var handle = p_361871_.get(resourcelocation); -+ -+ if (handle instanceof FrameGraphBuilder.Handle frameHandle -+ && frameHandle.getDescriptor() instanceof RenderTargetDescriptor renderDescriptor) { -+ useDepth |= renderDescriptor.useDepth(); -+ useStencil |= renderDescriptor.useStencil(); -+ } else { -+ var target = handle.get(); -+ useDepth |= target.useDepth; -+ useStencil |= target.useStencil; -+ } - } - - for (Entry entry : this.internalTargets.entrySet()) { +@@ -103,9 +_,9 @@ ResourceLocation resourcelocation1 = entry.getKey(); RenderTargetDescriptor rendertargetdescriptor = switch (entry.getValue()) { case PostChainConfig.FixedSizedTarget(int i, int j) -> { - yield new RenderTargetDescriptor(i, j, true); -+ yield new RenderTargetDescriptor(i, j, useDepth, useStencil); ++ yield new RenderTargetDescriptor(i, j, true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); } - case PostChainConfig.FullScreenTarget postchainconfig$fullscreentarget -> new RenderTargetDescriptor(p_361423_, p_362735_, true); -+ case PostChainConfig.FullScreenTarget postchainconfig$fullscreentarget -> new RenderTargetDescriptor(p_361423_, p_362735_, useDepth, useStencil); ++ case PostChainConfig.FullScreenTarget postchainconfig$fullscreentarget -> new RenderTargetDescriptor(p_361423_, p_362735_, true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); default -> throw new MatchException(null, null); }; map.put(resourcelocation1, p_362523_.createInternal(resourcelocation1.toString(), rendertargetdescriptor)); diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index 11d1307d91a..34142e0923b 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -1102,9 +1102,21 @@ public static FrameGraphSetupEvent fireFrameGraphSetup(FrameGraphBuilder builder return NeoForge.EVENT_BUS.post(new FrameGraphSetupEvent(builder, targets, renderTargetDescriptor, frustum, camera, modelViewMatrix, projectionMatrix, deltaTracker, profiler)); } - @ApiStatus.Internal - public static MainTarget createMainRenderTarget(int width, int height) { + @Nullable + private static Boolean enableStencil; + + public static void configureMainRenderTarget() { + if (enableStencil != null) { + throw new IllegalStateException("configureMainRenderTarget() called more than once"); + } var e = ModLoader.postEventWithReturn(new ConfigureMainRenderTargetEvent()); - return new MainTarget(width, height, e.useStencil()); + enableStencil = e.useStencil(); + } + + public static boolean isStencilEnabled() { + if (enableStencil == null) { + throw new IllegalStateException("isStencilEnabled() called before ConfigureMainRenderTargetEvent was dispatched"); + } + return enableStencil; } } From 7946bfc2154586ca5faf13d99c0b40c0367a1b4b Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:07:58 +0100 Subject: [PATCH 22/35] Split off mod construction and registry+config loading. Not sure it is necessary --- patches/net/minecraft/client/Minecraft.java.patch | 2 +- .../neoforge/client/loading/ClientModLoader.java | 12 ++++++++++-- .../neoforge/data/loading/DatagenModLoader.java | 1 + .../neoforged/neoforge/internal/CommonModLoader.java | 12 +++++++----- .../neoforge/server/loading/ServerModLoader.java | 1 + 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 30d5573d241..2d310b302eb 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -18,7 +18,7 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); -+ net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); ++ net.neoforged.neoforge.client.loading.ClientModLoader.constructMods(this); + net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(); this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); diff --git a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java index 4ee4d48b270..d3e6e914a1d 100644 --- a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java +++ b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java @@ -47,7 +47,7 @@ public class ClientModLoader extends CommonModLoader { @Nullable private static ModLoadingException error; - public static void begin(final Minecraft minecraft) { + public static void constructMods(Minecraft minecraft) { // force log4j to shutdown logging in a shutdown hook. This is because we disable default shutdown hook so the server properly logs it's shutdown Runtime.getRuntime().addShutdownHook(new Thread(LogManager::shutdown)); ImmediateWindowHandler.updateProgress("Loading mods"); @@ -56,13 +56,21 @@ public static void begin(final Minecraft minecraft) { LogicalSidedProvider.setClient(() -> minecraft); LanguageHook.loadBuiltinLanguages(); try { - begin(ImmediateWindowHandler::renderTick, false); + constructMods(ImmediateWindowHandler::renderTick); } catch (ModLoadingException e) { error = e; } } public static void finish(final PackRepository defaultResourcePacks, final ReloadableResourceManager mcResourceManager) { + if (error == null) { + try { + begin(ImmediateWindowHandler::renderTick, false); + } catch (ModLoadingException e) { + error = e; + } + } + if (error == null) { ResourcePackLoader.populatePackRepository(defaultResourcePacks, PackType.CLIENT_RESOURCES, false); DataPackConfig.DEFAULT.addModPacks(ResourcePackLoader.getPackNames(PackType.SERVER_DATA)); diff --git a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java index 6540788c64d..9241bfffc4f 100644 --- a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java +++ b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java @@ -43,6 +43,7 @@ public static void begin(final Set mods, final Path path, final Collecti LOGGER.info("Initializing Data Gatherer for mods {}", mods); runningDataGen = true; Bootstrap.bootStrap(); + constructMods(() -> {}); begin(() -> {}, true); // Modify components as the (modified) defaults may be required in datagen, i.e. stack size RegistrationEvents.modifyComponents(); diff --git a/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java b/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java index 583a4cb5796..8c3931c8cfc 100644 --- a/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java +++ b/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java @@ -30,9 +30,9 @@ * Internal class for handling the steps of mod loading that are common for client, data and server runs. * *

*/ @ApiStatus.Internal @@ -43,11 +43,13 @@ public static boolean areRegistriesLoaded() { return registriesLoaded; } + protected static void constructMods(Runnable periodicTask) { + ModLoader.gatherAndInitializeMods(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor(), periodicTask); + } + protected static void begin(Runnable periodicTask, boolean datagen) { var syncExecutor = ModWorkManager.syncExecutor(); - ModLoader.gatherAndInitializeMods(syncExecutor, ModWorkManager.parallelExecutor(), periodicTask); - ModLoader.runInitTask("Registry initialization", syncExecutor, periodicTask, () -> { RegistryManager.postNewRegistryEvent(); GameData.unfreezeData(); diff --git a/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java b/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java index 67820326016..37fe5199275 100644 --- a/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java +++ b/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java @@ -31,6 +31,7 @@ public static void load() { }); LanguageHook.loadBuiltinLanguages(); try { + constructMods(() -> {}); begin(() -> {}, false); load(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor()); finish(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor()); From 42746f0bdca0f52315e57d03daa0039d2b98f17d Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:09:15 +0100 Subject: [PATCH 23/35] Use global stencil state --- .../net/minecraft/client/renderer/LevelRenderer.java.patch | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/patches/net/minecraft/client/renderer/LevelRenderer.java.patch b/patches/net/minecraft/client/renderer/LevelRenderer.java.patch index 094b3d9dfee..c73119d6ff2 100644 --- a/patches/net/minecraft/client/renderer/LevelRenderer.java.patch +++ b/patches/net/minecraft/client/renderer/LevelRenderer.java.patch @@ -1,12 +1,11 @@ --- a/net/minecraft/client/renderer/LevelRenderer.java +++ b/net/minecraft/client/renderer/LevelRenderer.java -@@ -459,7 +_,8 @@ +@@ -459,7 +_,7 @@ this.targets.main = framegraphbuilder.importExternal("main", this.minecraft.getMainRenderTarget()); int i = this.minecraft.getMainRenderTarget().width; int j = this.minecraft.getMainRenderTarget().height; - RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true); -+ boolean useStencil = this.minecraft.getMainRenderTarget().useStencil; -+ RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true, useStencil); ++ RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); PostChain postchain = this.getTransparencyChain(); if (postchain != null) { this.targets.translucent = framegraphbuilder.createInternal("translucent", rendertargetdescriptor); From b70c8f797aacf0a2caa85f0f4aee48298e0aed29 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 15 Jan 2025 00:29:15 +0100 Subject: [PATCH 24/35] Add super basic test and make things work --- .../blaze3d/pipeline/MainTarget.java.patch | 11 ++-- .../blaze3d/pipeline/RenderTarget.java.patch | 4 +- .../neoforge/client/ClientHooks.java | 1 - .../client/rendering/StencilEnableTest.java | 58 ++++++++++++++++--- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 36aa1264950..e546cda921c 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -9,13 +9,12 @@ this.createFrameBuffer(p_166137_, p_166138_); } -@@ -37,6 +_,9 @@ +@@ -36,7 +_,7 @@ + GlStateManager._texParameter(3553, 10240, 9728); GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); - GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); -+ if (this.useStencil) { -+ GlStateManager._glFramebufferTexture2D(36160, org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, 3553, this.depthBufferId, 0); -+ } +- GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); ++ GlStateManager._glFramebufferTexture2D(36160, this.useStencil ? org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL_ATTACHMENT : 36096, 3553, this.depthBufferId, 0); GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; @@ -25,7 +24,7 @@ GlStateManager._bindTexture(this.depthBufferId); - GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); + if (this.useStencil) { -+ GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, null); ++ GlStateManager._texImage2D(3553, 0, org.lwjgl.opengl.GL30.GL_DEPTH32F_STENCIL8, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, org.lwjgl.opengl.GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, null); + } else { + GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); + } diff --git a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch index 4d6619c9184..f55013f74a3 100644 --- a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch @@ -37,11 +37,11 @@ + GlStateManager._texImage2D( + org.lwjgl.opengl.GL32.GL_TEXTURE_2D, + 0, -+ org.lwjgl.opengl.GL32.GL_DEPTH24_STENCIL8, ++ org.lwjgl.opengl.GL32.GL_DEPTH32F_STENCIL8, + this.width, this.height, + 0, + org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, -+ org.lwjgl.opengl.GL32.GL_UNSIGNED_INT_24_8, ++ org.lwjgl.opengl.GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, + null); + } } diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index 34142e0923b..d6b05d8bf4c 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -9,7 +9,6 @@ import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableMap; import com.mojang.blaze3d.framegraph.FrameGraphBuilder; -import com.mojang.blaze3d.pipeline.MainTarget; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.Window; import com.mojang.blaze3d.resource.RenderTargetDescriptor; diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java index 410efb7a9a6..dfef5601c89 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java @@ -5,20 +5,62 @@ package net.neoforged.neoforge.oldtest.client.rendering; +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.Blocks; +import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.common.Mod; -import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent; +import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent; +import org.lwjgl.opengl.GL33; -@Mod("stencil_enable_test") +@Mod(value = StencilEnableTest.MOD_ID, dist = Dist.CLIENT) public class StencilEnableTest { - public static boolean ENABLED = true; + public static final String MOD_ID = "stencil_enable_test"; + public static final boolean ENABLED = true; public StencilEnableTest(IEventBus modEventBus) { - modEventBus.addListener(this::clientSetup); - } + if (!ENABLED) { + return; + } + modEventBus.addListener(ConfigureMainRenderTargetEvent.class, event -> { + event.enableStencil(); + }); + modEventBus.addListener(RegisterGuiLayersEvent.class, event -> { + event.registerAboveAll( + ResourceLocation.fromNamespaceAndPath(MOD_ID, "block_outline"), + (guiGraphics, delta) -> { + guiGraphics.pose().pushPose(); + guiGraphics.pose().translate(10, 10, 0); + + RenderSystem.clear(GL33.GL_STENCIL_BUFFER_BIT); + + GL33.glEnable(GL33.GL_STENCIL_TEST); + GL33.glStencilOp(GL33.GL_KEEP, GL33.GL_KEEP, GL33.GL_REPLACE); + GL33.glStencilFunc(GL33.GL_ALWAYS, 1, 0xFF); + GL33.glStencilMask(0xFF); + + var stack = new ItemStack(Blocks.GRASS_BLOCK); + guiGraphics.renderItem(stack, 0, 0); + guiGraphics.renderItem(stack, 10, 10); + + GL33.glStencilFunc(GL33.GL_NOTEQUAL, 1, 0xFF); + GL33.glStencilMask(0x00); + RenderSystem.disableDepthTest(); + + stack = new ItemStack(Blocks.DIAMOND_BLOCK); + guiGraphics.pose().scale(1.1f, 1.1f, 1.1f); + guiGraphics.pose().translate(-1, -1, -1); + guiGraphics.renderItem(stack, 0, 0); + guiGraphics.renderItem(stack, 10, 10); + + RenderSystem.enableDepthTest(); + GL33.glDisable(GL33.GL_STENCIL_TEST); - private void clientSetup(FMLClientSetupEvent event) { - // TODO 1.21.2 if (ENABLED) - // TODO 1.21.2 event.enqueueWork(() -> Minecraft.getInstance().getMainRenderTarget().enableStencil()); + guiGraphics.pose().popPose(); + }); + }); } } From d2ed447d81bd65fc28a296be2ab771139e434e40 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:41:51 +0100 Subject: [PATCH 25/35] Document stencil test, and prefer RenderSystem or GL30 to GL33 --- .../client/rendering/StencilEnableTest.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java index dfef5601c89..a2e4092e409 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java @@ -14,8 +14,12 @@ import net.neoforged.fml.common.Mod; import net.neoforged.neoforge.client.event.ConfigureMainRenderTargetEvent; import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent; -import org.lwjgl.opengl.GL33; +import org.lwjgl.opengl.GL30; +/** + * Basic test that uses the stencil buffer. + * When the test is enabled, it will render two grass blocks with a diamond block outline in the top left corner of the screen. + */ @Mod(value = StencilEnableTest.MOD_ID, dist = Dist.CLIENT) public class StencilEnableTest { public static final String MOD_ID = "stencil_enable_test"; @@ -35,19 +39,21 @@ public StencilEnableTest(IEventBus modEventBus) { guiGraphics.pose().pushPose(); guiGraphics.pose().translate(10, 10, 0); - RenderSystem.clear(GL33.GL_STENCIL_BUFFER_BIT); + // Implementation derived from https://learnopengl.com/Advanced-OpenGL/Stencil-testing, + // but outlining with a block of diamond rather than a fixed color. + RenderSystem.clear(GL30.GL_STENCIL_BUFFER_BIT); - GL33.glEnable(GL33.GL_STENCIL_TEST); - GL33.glStencilOp(GL33.GL_KEEP, GL33.GL_KEEP, GL33.GL_REPLACE); - GL33.glStencilFunc(GL33.GL_ALWAYS, 1, 0xFF); - GL33.glStencilMask(0xFF); + GL30.glEnable(GL30.GL_STENCIL_TEST); + RenderSystem.stencilOp(GL30.GL_KEEP, GL30.GL_KEEP, GL30.GL_REPLACE); + RenderSystem.stencilFunc(GL30.GL_ALWAYS, 1, 0xFF); + RenderSystem.stencilMask(0xFF); var stack = new ItemStack(Blocks.GRASS_BLOCK); guiGraphics.renderItem(stack, 0, 0); guiGraphics.renderItem(stack, 10, 10); - GL33.glStencilFunc(GL33.GL_NOTEQUAL, 1, 0xFF); - GL33.glStencilMask(0x00); + RenderSystem.stencilFunc(GL30.GL_NOTEQUAL, 1, 0xFF); + RenderSystem.stencilMask(0x00); RenderSystem.disableDepthTest(); stack = new ItemStack(Blocks.DIAMOND_BLOCK); @@ -57,7 +63,7 @@ public StencilEnableTest(IEventBus modEventBus) { guiGraphics.renderItem(stack, 10, 10); RenderSystem.enableDepthTest(); - GL33.glDisable(GL33.GL_STENCIL_TEST); + GL30.glDisable(GL30.GL_STENCIL_TEST); guiGraphics.pose().popPose(); }); From 5f4bd67bc88fe6dde832d590be377a23ee6f60a6 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:17:24 +0100 Subject: [PATCH 26/35] Align MainTarget and RenderTarget patches --- .../blaze3d/pipeline/MainTarget.java.patch | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index e546cda921c..84aa701f306 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -9,24 +9,39 @@ this.createFrameBuffer(p_166137_, p_166138_); } -@@ -36,7 +_,7 @@ - GlStateManager._texParameter(3553, 10240, 9728); +@@ -37,6 +_,14 @@ GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); -- GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); -+ GlStateManager._glFramebufferTexture2D(36160, this.useStencil ? org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL_ATTACHMENT : 36096, 3553, this.depthBufferId, 0); + GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, this.depthBufferId, 0); ++ if (this.useStencil) { ++ GlStateManager._glFramebufferTexture2D( ++ org.lwjgl.opengl.GL32.GL_FRAMEBUFFER, ++ org.lwjgl.opengl.GL32.GL_STENCIL_ATTACHMENT, ++ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, ++ this.depthBufferId, ++ 0); ++ } GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; -@@ -82,7 +_,11 @@ +@@ -82,7 +_,20 @@ RenderSystem.assertOnRenderThreadOrInit(); GlStateManager._getError(); GlStateManager._bindTexture(this.depthBufferId); - GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); -+ if (this.useStencil) { -+ GlStateManager._texImage2D(3553, 0, org.lwjgl.opengl.GL30.GL_DEPTH32F_STENCIL8, p_166145_.width, p_166145_.height, 0, org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, org.lwjgl.opengl.GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, null); -+ } else { ++ if (!this.useStencil) { + GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); ++ } else { ++ // Use a combined format for both depth and stencil. ++ GlStateManager._texImage2D( ++ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, ++ 0, ++ org.lwjgl.opengl.GL32.GL_DEPTH32F_STENCIL8, ++ this.width, this.height, ++ 0, ++ org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, ++ org.lwjgl.opengl.GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, ++ null); + } return GlStateManager._getError() != 1285; } From d5c0198b55f29c2c0ddf7e10b9df7f5cedb812f6 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:17:59 +0100 Subject: [PATCH 27/35] Revert "Split off mod construction and registry+config loading. Not sure it is necessary" This reverts commit 7946bfc2154586ca5faf13d99c0b40c0367a1b4b. --- patches/net/minecraft/client/Minecraft.java.patch | 2 +- .../neoforge/client/loading/ClientModLoader.java | 12 ++---------- .../neoforge/data/loading/DatagenModLoader.java | 1 - .../neoforged/neoforge/internal/CommonModLoader.java | 12 +++++------- .../neoforge/server/loading/ServerModLoader.java | 1 - 5 files changed, 8 insertions(+), 20 deletions(-) diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 2d310b302eb..30d5573d241 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -18,7 +18,7 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); -+ net.neoforged.neoforge.client.loading.ClientModLoader.constructMods(this); ++ net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); + net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(); this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); diff --git a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java index d3e6e914a1d..4ee4d48b270 100644 --- a/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java +++ b/src/main/java/net/neoforged/neoforge/client/loading/ClientModLoader.java @@ -47,7 +47,7 @@ public class ClientModLoader extends CommonModLoader { @Nullable private static ModLoadingException error; - public static void constructMods(Minecraft minecraft) { + public static void begin(final Minecraft minecraft) { // force log4j to shutdown logging in a shutdown hook. This is because we disable default shutdown hook so the server properly logs it's shutdown Runtime.getRuntime().addShutdownHook(new Thread(LogManager::shutdown)); ImmediateWindowHandler.updateProgress("Loading mods"); @@ -56,21 +56,13 @@ public static void constructMods(Minecraft minecraft) { LogicalSidedProvider.setClient(() -> minecraft); LanguageHook.loadBuiltinLanguages(); try { - constructMods(ImmediateWindowHandler::renderTick); + begin(ImmediateWindowHandler::renderTick, false); } catch (ModLoadingException e) { error = e; } } public static void finish(final PackRepository defaultResourcePacks, final ReloadableResourceManager mcResourceManager) { - if (error == null) { - try { - begin(ImmediateWindowHandler::renderTick, false); - } catch (ModLoadingException e) { - error = e; - } - } - if (error == null) { ResourcePackLoader.populatePackRepository(defaultResourcePacks, PackType.CLIENT_RESOURCES, false); DataPackConfig.DEFAULT.addModPacks(ResourcePackLoader.getPackNames(PackType.SERVER_DATA)); diff --git a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java index 9241bfffc4f..6540788c64d 100644 --- a/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java +++ b/src/main/java/net/neoforged/neoforge/data/loading/DatagenModLoader.java @@ -43,7 +43,6 @@ public static void begin(final Set mods, final Path path, final Collecti LOGGER.info("Initializing Data Gatherer for mods {}", mods); runningDataGen = true; Bootstrap.bootStrap(); - constructMods(() -> {}); begin(() -> {}, true); // Modify components as the (modified) defaults may be required in datagen, i.e. stack size RegistrationEvents.modifyComponents(); diff --git a/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java b/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java index 8c3931c8cfc..583a4cb5796 100644 --- a/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java +++ b/src/main/java/net/neoforged/neoforge/internal/CommonModLoader.java @@ -30,9 +30,9 @@ * Internal class for handling the steps of mod loading that are common for client, data and server runs. * *

    - *
  • Client runs {@link #constructMods}, {@link #begin}, {@link #load} and {@link #finish} at different timings, see {@link ClientModLoader}.
  • - *
  • Server runs all 4 consecutively.
  • - *
  • Datagen only runs {@link #constructMods} and {@link #begin}.
  • + *
  • Client runs {@link #begin}, {@link #load} and {@link #finish} at different timings, see {@link ClientModLoader}.
  • + *
  • Server runs all 3 consecutively.
  • + *
  • Datagen only runs {@link #begin}.
  • *
*/ @ApiStatus.Internal @@ -43,13 +43,11 @@ public static boolean areRegistriesLoaded() { return registriesLoaded; } - protected static void constructMods(Runnable periodicTask) { - ModLoader.gatherAndInitializeMods(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor(), periodicTask); - } - protected static void begin(Runnable periodicTask, boolean datagen) { var syncExecutor = ModWorkManager.syncExecutor(); + ModLoader.gatherAndInitializeMods(syncExecutor, ModWorkManager.parallelExecutor(), periodicTask); + ModLoader.runInitTask("Registry initialization", syncExecutor, periodicTask, () -> { RegistryManager.postNewRegistryEvent(); GameData.unfreezeData(); diff --git a/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java b/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java index 37fe5199275..67820326016 100644 --- a/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java +++ b/src/main/java/net/neoforged/neoforge/server/loading/ServerModLoader.java @@ -31,7 +31,6 @@ public static void load() { }); LanguageHook.loadBuiltinLanguages(); try { - constructMods(() -> {}); begin(() -> {}, false); load(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor()); finish(ModWorkManager.syncExecutor(), ModWorkManager.parallelExecutor()); From 2ea4992816f4a25fb64ee9b175a90c8391002cc0 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:21:00 +0100 Subject: [PATCH 28/35] Remove now-unneeded ATs --- src/main/resources/META-INF/accesstransformer.cfg | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 1f4ce258fa3..fc21dedc592 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -1,8 +1,4 @@ # Note: This file is for manually added ATs. When AT entries can be programmatically generated based on fixed rules you may define those rules in the build.gradle file -public com.mojang.blaze3d.framegraph.FrameGraphBuilder$Handle -public com.mojang.blaze3d.framegraph.FrameGraphBuilder$Handle holder -public com.mojang.blaze3d.framegraph.FrameGraphBuilder$VirtualResource -public com.mojang.blaze3d.framegraph.FrameGraphBuilder$Pass public net.minecraft.advancements.CriteriaTriggers register(Ljava/lang/String;Lnet/minecraft/advancements/CriterionTrigger;)Lnet/minecraft/advancements/CriterionTrigger; # register default net.minecraft.client.KeyMapping isDown # isDown public-f net.minecraft.client.Options keyMappings # keyMappings From 2bbe87583502bcdb9203bd952bb9681ba69cf594 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:46:54 +0100 Subject: [PATCH 29/35] Add config option to choose between 24+8 and 32+8 formats --- .../blaze3d/pipeline/MainTarget.java.patch | 13 ++---------- .../blaze3d/pipeline/RenderTarget.java.patch | 13 ++---------- .../neoforge/client/ClientHooks.java | 21 +++++++++++++++++++ .../neoforge/common/NeoForgeConfig.java | 7 +++++++ .../resources/assets/neoforge/lang/en_us.json | 2 ++ 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index 84aa701f306..e320aa4caf3 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -24,7 +24,7 @@ GlStateManager._bindTexture(0); this.viewWidth = maintarget$dimension.width; this.viewHeight = maintarget$dimension.height; -@@ -82,7 +_,20 @@ +@@ -82,7 +_,11 @@ RenderSystem.assertOnRenderThreadOrInit(); GlStateManager._getError(); GlStateManager._bindTexture(this.depthBufferId); @@ -32,16 +32,7 @@ + if (!this.useStencil) { + GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); + } else { -+ // Use a combined format for both depth and stencil. -+ GlStateManager._texImage2D( -+ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, -+ 0, -+ org.lwjgl.opengl.GL32.GL_DEPTH32F_STENCIL8, -+ this.width, this.height, -+ 0, -+ org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, -+ org.lwjgl.opengl.GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, -+ null); ++ net.neoforged.neoforge.client.ClientHooks.texImageDepthStencil(p_166145_.width, p_166145_.height); + } return GlStateManager._getError() != 1285; } diff --git a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch index f55013f74a3..f072b12eeaa 100644 --- a/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch @@ -25,7 +25,7 @@ this.frameBufferId = -1; this.colorTextureId = -1; this.depthBufferId = -1; -@@ -96,7 +_,20 @@ +@@ -96,7 +_,11 @@ GlStateManager._texParameter(3553, 34892, 0); GlStateManager._texParameter(3553, 10242, 33071); GlStateManager._texParameter(3553, 10243, 33071); @@ -33,16 +33,7 @@ + if (!this.useStencil) { + GlStateManager._texImage2D(3553, 0, 6402, this.width, this.height, 0, 6402, 5126, null); + } else { -+ // Use a combined format for both depth and stencil. -+ GlStateManager._texImage2D( -+ org.lwjgl.opengl.GL32.GL_TEXTURE_2D, -+ 0, -+ org.lwjgl.opengl.GL32.GL_DEPTH32F_STENCIL8, -+ this.width, this.height, -+ 0, -+ org.lwjgl.opengl.GL32.GL_DEPTH_STENCIL, -+ org.lwjgl.opengl.GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, -+ null); ++ net.neoforged.neoforge.client.ClientHooks.texImageDepthStencil(this.width, this.height); + } } diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index d6b05d8bf4c..61f883ec901 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -9,6 +9,7 @@ import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableMap; import com.mojang.blaze3d.framegraph.FrameGraphBuilder; +import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.Window; import com.mojang.blaze3d.resource.RenderTargetDescriptor; @@ -183,6 +184,7 @@ import net.neoforged.neoforge.client.model.data.ModelData; import net.neoforged.neoforge.client.renderstate.RegisterRenderStateModifiersEvent; import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.common.NeoForgeConfig; import net.neoforged.neoforge.common.NeoForgeMod; import net.neoforged.neoforge.forge.snapshots.ForgeSnapshotsModClient; import net.neoforged.neoforge.gametest.GameTestHooks; @@ -196,6 +198,7 @@ import org.joml.Matrix4f; import org.joml.Vector3f; import org.joml.Vector4f; +import org.lwjgl.opengl.GL32; /** * Class for various client-side-only hooks. @@ -1104,6 +1107,7 @@ public static FrameGraphSetupEvent fireFrameGraphSetup(FrameGraphBuilder builder @Nullable private static Boolean enableStencil; + @ApiStatus.Internal public static void configureMainRenderTarget() { if (enableStencil != null) { throw new IllegalStateException("configureMainRenderTarget() called more than once"); @@ -1118,4 +1122,21 @@ public static boolean isStencilEnabled() { } return enableStencil; } + + /** + * Called by our stencil hooks to specify the depth+stencil texture. + */ + @ApiStatus.Internal + public static void texImageDepthStencil(int width, int height) { + var reducedPrecision = NeoForgeConfig.CLIENT.reducedDepthStencilFormat.getAsBoolean(); + GlStateManager._texImage2D( + GL32.GL_TEXTURE_2D, + 0, + reducedPrecision ? GL32.GL_DEPTH24_STENCIL8 : GL32.GL_DEPTH32F_STENCIL8, + width, height, + 0, + GL32.GL_DEPTH_STENCIL, + reducedPrecision ? GL32.GL_UNSIGNED_INT_24_8 : GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, + null); + } } diff --git a/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java b/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java index e7335e0b74b..b48fe0110f4 100644 --- a/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java +++ b/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java @@ -99,6 +99,8 @@ public static class Client { public final BooleanValue logUntranslatedConfigurationWarnings; + public final BooleanValue reducedDepthStencilFormat; + Client(ModConfigSpec.Builder builder) { experimentalForgeLightPipelineEnabled = builder .comment("EXPERIMENTAL: Enable the NeoForge block rendering pipeline - fixes the lighting of custom models.") @@ -114,6 +116,11 @@ public static class Client { .comment("A config option mainly for developers. Logs out configuration values that do not have translations when running a client in a development environment.") .translation("neoforge.configgui.logUntranslatedConfigurationWarnings") .define("logUntranslatedConfigurationWarnings", true); + + reducedDepthStencilFormat = builder + .comment("Configures how many bits are used for the depth buffer when stenciling has been enabled by a mod. Set to true for 24+8 bits and to false for 32+8 bits. Setting to true might slightly improve performance, but risks introducing visual artifacts.") + .translation("neoforge.configgui.reducedDepthStencilFormat") + .define("reducedDepthStencilFormat", false); } } diff --git a/src/main/resources/assets/neoforge/lang/en_us.json b/src/main/resources/assets/neoforge/lang/en_us.json index 7cd26fc40b5..def3c9b77d6 100644 --- a/src/main/resources/assets/neoforge/lang/en_us.json +++ b/src/main/resources/assets/neoforge/lang/en_us.json @@ -206,6 +206,8 @@ "neoforge.configgui.logUntranslatedItemTagWarnings.tooltip": "A config option mainly for developers. Logs out modded item tags that do not have translations when running on integrated server. Format desired is tag.item.. for the translation key. Defaults to SILENCED.", "neoforge.configgui.permissionHandler": "Permission Handler", "neoforge.configgui.permissionHandler.tooltip": "The permission handler used by the server. Defaults to neoforge:default_handler if no such handler with that name is registered.", + "neoforge.configgui.reducedDepthStencilFormat": "Reduced Depth+Stencil Format", + "neoforge.configgui.reducedDepthStencilFormat.tooltip": "Configures how many bits are used for the depth buffer when stenciling has been enabled by a mod. Set to true for 24+8 bits and to false for 32+8 bits. Setting to true might slightly improve performance, but risks introducing visual artifacts.", "neoforge.configgui.removeErroringBlockEntities": "Remove Erroring Block Entities", "neoforge.configgui.removeErroringBlockEntities.tooltip": "Set this to true to remove any BlockEntity that throws an error in its update method instead of closing the server and reporting a crash log.", "neoforge.configgui.removeErroringBlockEntities.tooltip.warning": "BE WARNED THIS COULD SCREW UP EVERYTHING.\nUSE SPARINGLY.\nWE ARE NOT RESPONSIBLE FOR DAMAGES.", From 9de54d23ebab7589e048288602c6778a297942d0 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 16 Jan 2025 00:27:28 +0100 Subject: [PATCH 30/35] Remove rendertarget != null check --- patches/net/minecraft/client/Minecraft.java.patch | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 30d5573d241..9a0e725c5d0 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -179,7 +179,7 @@ this.deltaTracker.updatePauseState(this.pause); this.deltaTracker.updateFrozenState(!this.isLevelRunningNormally()); long l = Util.getNanos(); -@@ -1351,10 +_,13 @@ +@@ -1351,10 +_,12 @@ this.window.setGuiScale((double)i); if (this.screen != null) { this.screen.resize(this, this.window.getGuiScaledWidth(), this.window.getGuiScaledHeight()); @@ -187,7 +187,6 @@ } RenderTarget rendertarget = this.getMainRenderTarget(); -+ if (rendertarget != null) rendertarget.resize(this.window.getWidth(), this.window.getHeight()); + if (this.gameRenderer != null) this.gameRenderer.resize(this.window.getWidth(), this.window.getHeight()); From 7f4952dde868f438ccb633824734e3fe4d386171 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 16 Jan 2025 00:31:15 +0100 Subject: [PATCH 31/35] add small clarification --- src/main/java/net/neoforged/neoforge/client/ClientHooks.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index 61f883ec901..525960cfb31 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -1136,6 +1136,8 @@ public static void texImageDepthStencil(int width, int height) { width, height, 0, GL32.GL_DEPTH_STENCIL, + // Since data is null, the format here does not matter as long as it matches internalFormat. + // So the usage, for depth, of unsigned int in one case and float in the other case, is not a problem. reducedPrecision ? GL32.GL_UNSIGNED_INT_24_8 : GL32.GL_FLOAT_32_UNSIGNED_INT_24_8_REV, null); } From 76b0d06e2439deb1f7532e5161b1a9acc54ac44b Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 16 Jan 2025 00:54:02 +0100 Subject: [PATCH 32/35] Fix test breaking screen rendering and semi-disable by default --- .../client/rendering/StencilEnableTest.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java index a2e4092e409..16e40446a8b 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java @@ -23,16 +23,30 @@ @Mod(value = StencilEnableTest.MOD_ID, dist = Dist.CLIENT) public class StencilEnableTest { public static final String MOD_ID = "stencil_enable_test"; - public static final boolean ENABLED = true; + private enum State { + DISABLE, + /** + * Enables stencil buffer, but does not perform any rendering with stencil. + */ + ENABLE_REGISTRATION, + /** + * Enables stencil buffer, and renders an overlay using stencil. + */ + ENABLE_UI_LAYER, + } + private static final State ENABLED = State.ENABLE_REGISTRATION; public StencilEnableTest(IEventBus modEventBus) { - if (!ENABLED) { + if (ENABLED == State.DISABLE) { return; } modEventBus.addListener(ConfigureMainRenderTargetEvent.class, event -> { event.enableStencil(); }); modEventBus.addListener(RegisterGuiLayersEvent.class, event -> { + if (ENABLED != State.ENABLE_UI_LAYER) { + return; + } event.registerAboveAll( ResourceLocation.fromNamespaceAndPath(MOD_ID, "block_outline"), (guiGraphics, delta) -> { @@ -54,7 +68,6 @@ public StencilEnableTest(IEventBus modEventBus) { RenderSystem.stencilFunc(GL30.GL_NOTEQUAL, 1, 0xFF); RenderSystem.stencilMask(0x00); - RenderSystem.disableDepthTest(); stack = new ItemStack(Blocks.DIAMOND_BLOCK); guiGraphics.pose().scale(1.1f, 1.1f, 1.1f); @@ -62,7 +75,6 @@ public StencilEnableTest(IEventBus modEventBus) { guiGraphics.renderItem(stack, 0, 0); guiGraphics.renderItem(stack, 10, 10); - RenderSystem.enableDepthTest(); GL30.glDisable(GL30.GL_STENCIL_TEST); guiGraphics.pose().popPose(); From 2a2b00aa744e505f0a8e5ae6be4f42ac8cfe3bec Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 16 Jan 2025 21:57:08 +0100 Subject: [PATCH 33/35] Immaculate --- .../neoforge/oldtest/client/rendering/StencilEnableTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java index 16e40446a8b..cc91b6ac988 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/rendering/StencilEnableTest.java @@ -23,6 +23,7 @@ @Mod(value = StencilEnableTest.MOD_ID, dist = Dist.CLIENT) public class StencilEnableTest { public static final String MOD_ID = "stencil_enable_test"; + private enum State { DISABLE, /** @@ -34,6 +35,7 @@ private enum State { */ ENABLE_UI_LAYER, } + private static final State ENABLED = State.ENABLE_REGISTRATION; public StencilEnableTest(IEventBus modEventBus) { From 057ecba1ef42405b88da83fe70cfa7ff5018d043 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:55:36 +0100 Subject: [PATCH 34/35] Address review comments --- .../mojang/blaze3d/pipeline/MainTarget.java.patch | 7 +++++-- patches/net/minecraft/client/Minecraft.java.patch | 3 ++- .../neoforged/neoforge/client/ClientHooks.java | 2 +- .../event/ConfigureMainRenderTargetEvent.java | 15 ++++++--------- .../neoforged/neoforge/common/NeoForgeConfig.java | 2 +- .../resources/assets/neoforge/lang/en_us.json | 2 +- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch index e320aa4caf3..f6172f2050c 100644 --- a/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch +++ b/patches/com/mojang/blaze3d/pipeline/MainTarget.java.patch @@ -1,11 +1,14 @@ --- a/com/mojang/blaze3d/pipeline/MainTarget.java +++ b/com/mojang/blaze3d/pipeline/MainTarget.java -@@ -16,7 +_,7 @@ +@@ -16,7 +_,10 @@ static final MainTarget.Dimension DEFAULT_DIMENSIONS = new MainTarget.Dimension(854, 480); public MainTarget(int p_166137_, int p_166138_) { - super(true); -+ super(true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); ++ this(p_166137_, p_166138_, false); ++ } ++ public MainTarget(int p_166137_, int p_166138_, boolean enableStencil) { ++ super(true, enableStencil); this.createFrameBuffer(p_166137_, p_166138_); } diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 9a0e725c5d0..590f8f2c0fc 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -18,9 +18,10 @@ this.keyboardHandler = new KeyboardHandler(this); - this.keyboardHandler.setup(this.window.getWindow()); RenderSystem.initRenderer(this.options.glDebugVerbosity, false); +- this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); + net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); + net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(); - this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); ++ this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight(), net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); this.mainRenderTarget.clear(); this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index 525960cfb31..40174e85fee 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -1113,7 +1113,7 @@ public static void configureMainRenderTarget() { throw new IllegalStateException("configureMainRenderTarget() called more than once"); } var e = ModLoader.postEventWithReturn(new ConfigureMainRenderTargetEvent()); - enableStencil = e.useStencil(); + enableStencil = e.isStencilEnabled(); } public static boolean isStencilEnabled() { diff --git a/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java index 92b13184584..a5a7a2cc9b3 100644 --- a/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java +++ b/src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java @@ -19,24 +19,21 @@ * This event is fired on the mod-specific event bus, only on the {@linkplain LogicalSide#CLIENT logical client}. */ public class ConfigureMainRenderTargetEvent extends Event implements IModBusEvent { - private boolean useStencil; + private boolean enableStencil; /** * Returns whether enabling the stencil buffer on the main render target was requested. * * @return true, if the stencil buffer is enabled, or false otherwise. */ - public boolean useStencil() { - return this.useStencil; + public boolean isStencilEnabled() { + return this.enableStencil; } /** - * Enable the stencil buffer for the main render target. - * - * @return this, for method chaining. + * Enables the stencil buffer for the main render target. */ - public ConfigureMainRenderTargetEvent enableStencil() { - this.useStencil = true; - return this; + public void enableStencil() { + this.enableStencil = true; } } diff --git a/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java b/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java index b48fe0110f4..6f1f69bffa0 100644 --- a/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java +++ b/src/main/java/net/neoforged/neoforge/common/NeoForgeConfig.java @@ -118,7 +118,7 @@ public static class Client { .define("logUntranslatedConfigurationWarnings", true); reducedDepthStencilFormat = builder - .comment("Configures how many bits are used for the depth buffer when stenciling has been enabled by a mod. Set to true for 24+8 bits and to false for 32+8 bits. Setting to true might slightly improve performance, but risks introducing visual artifacts.") + .comment("Configures how many bits are used for the depth buffer when stenciling has been enabled by a mod. Set to true for 24+8 bits and to false for 32+8 bits. Setting to true will slightly reduce VRAM usage, but risks introducing visual artifacts.") .translation("neoforge.configgui.reducedDepthStencilFormat") .define("reducedDepthStencilFormat", false); } diff --git a/src/main/resources/assets/neoforge/lang/en_us.json b/src/main/resources/assets/neoforge/lang/en_us.json index def3c9b77d6..d7cb2fce90d 100644 --- a/src/main/resources/assets/neoforge/lang/en_us.json +++ b/src/main/resources/assets/neoforge/lang/en_us.json @@ -207,7 +207,7 @@ "neoforge.configgui.permissionHandler": "Permission Handler", "neoforge.configgui.permissionHandler.tooltip": "The permission handler used by the server. Defaults to neoforge:default_handler if no such handler with that name is registered.", "neoforge.configgui.reducedDepthStencilFormat": "Reduced Depth+Stencil Format", - "neoforge.configgui.reducedDepthStencilFormat.tooltip": "Configures how many bits are used for the depth buffer when stenciling has been enabled by a mod. Set to true for 24+8 bits and to false for 32+8 bits. Setting to true might slightly improve performance, but risks introducing visual artifacts.", + "neoforge.configgui.reducedDepthStencilFormat.tooltip": "Configures how many bits are used for the depth buffer when stenciling has been enabled by a mod. Set to true for 24+8 bits and to false for 32+8 bits. Setting to true will slightly reduce VRAM usage, but risks introducing visual artifacts.", "neoforge.configgui.removeErroringBlockEntities": "Remove Erroring Block Entities", "neoforge.configgui.removeErroringBlockEntities.tooltip": "Set this to true to remove any BlockEntity that throws an error in its update method instead of closing the server and reporting a crash log.", "neoforge.configgui.removeErroringBlockEntities.tooltip.warning": "BE WARNED THIS COULD SCREW UP EVERYTHING.\nUSE SPARINGLY.\nWE ARE NOT RESPONSIBLE FOR DAMAGES.", From e8f3e144f2701e62c1469de68b9c40a78f9bfac3 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Fri, 17 Jan 2025 00:09:11 +0100 Subject: [PATCH 35/35] Do not use stencil for PostChain internal buffers since it is not usable by pass shaders. Allowed me to simplify the event structure a lot. --- .../net/minecraft/client/Minecraft.java.patch | 5 ++--- .../client/renderer/LevelRenderer.java.patch | 2 +- .../client/renderer/PostChain.java.patch | 14 -------------- .../neoforged/neoforge/client/ClientHooks.java | 18 +++--------------- 4 files changed, 6 insertions(+), 33 deletions(-) delete mode 100644 patches/net/minecraft/client/renderer/PostChain.java.patch diff --git a/patches/net/minecraft/client/Minecraft.java.patch b/patches/net/minecraft/client/Minecraft.java.patch index 590f8f2c0fc..685b4601f0d 100644 --- a/patches/net/minecraft/client/Minecraft.java.patch +++ b/patches/net/minecraft/client/Minecraft.java.patch @@ -8,7 +8,7 @@ this.demo = p_91084_.game.demo; this.allowsMultiplayer = !p_91084_.game.disableMultiplayer; this.allowsChat = !p_91084_.game.disableChat; -@@ -483,15 +_,19 @@ +@@ -483,15 +_,18 @@ LOGGER.error("Couldn't set icon", (Throwable)ioexception); } @@ -20,8 +20,7 @@ RenderSystem.initRenderer(this.options.glDebugVerbosity, false); - this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight()); + net.neoforged.neoforge.client.loading.ClientModLoader.begin(this); -+ net.neoforged.neoforge.client.ClientHooks.configureMainRenderTarget(); -+ this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight(), net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); ++ this.mainRenderTarget = net.neoforged.neoforge.client.ClientHooks.instantiateMainTarget(this.window.getWidth(), this.window.getHeight()); this.mainRenderTarget.setClearColor(0.0F, 0.0F, 0.0F, 0.0F); this.mainRenderTarget.clear(); this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); diff --git a/patches/net/minecraft/client/renderer/LevelRenderer.java.patch b/patches/net/minecraft/client/renderer/LevelRenderer.java.patch index c73119d6ff2..f5d0540c5cc 100644 --- a/patches/net/minecraft/client/renderer/LevelRenderer.java.patch +++ b/patches/net/minecraft/client/renderer/LevelRenderer.java.patch @@ -5,7 +5,7 @@ int i = this.minecraft.getMainRenderTarget().width; int j = this.minecraft.getMainRenderTarget().height; - RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true); -+ RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); ++ RenderTargetDescriptor rendertargetdescriptor = new RenderTargetDescriptor(i, j, true, this.minecraft.getMainRenderTarget().useStencil); PostChain postchain = this.getTransparencyChain(); if (postchain != null) { this.targets.translucent = framegraphbuilder.createInternal("translucent", rendertargetdescriptor); diff --git a/patches/net/minecraft/client/renderer/PostChain.java.patch b/patches/net/minecraft/client/renderer/PostChain.java.patch deleted file mode 100644 index adeeadd6147..00000000000 --- a/patches/net/minecraft/client/renderer/PostChain.java.patch +++ /dev/null @@ -1,14 +0,0 @@ ---- a/net/minecraft/client/renderer/PostChain.java -+++ b/net/minecraft/client/renderer/PostChain.java -@@ -103,9 +_,9 @@ - ResourceLocation resourcelocation1 = entry.getKey(); - RenderTargetDescriptor rendertargetdescriptor = switch (entry.getValue()) { - case PostChainConfig.FixedSizedTarget(int i, int j) -> { -- yield new RenderTargetDescriptor(i, j, true); -+ yield new RenderTargetDescriptor(i, j, true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); - } -- case PostChainConfig.FullScreenTarget postchainconfig$fullscreentarget -> new RenderTargetDescriptor(p_361423_, p_362735_, true); -+ case PostChainConfig.FullScreenTarget postchainconfig$fullscreentarget -> new RenderTargetDescriptor(p_361423_, p_362735_, true, net.neoforged.neoforge.client.ClientHooks.isStencilEnabled()); - default -> throw new MatchException(null, null); - }; - map.put(resourcelocation1, p_362523_.createInternal(resourcelocation1.toString(), rendertargetdescriptor)); diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index 40174e85fee..97daa723753 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -9,6 +9,7 @@ import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableMap; import com.mojang.blaze3d.framegraph.FrameGraphBuilder; +import com.mojang.blaze3d.pipeline.MainTarget; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.Window; @@ -1104,23 +1105,10 @@ public static FrameGraphSetupEvent fireFrameGraphSetup(FrameGraphBuilder builder return NeoForge.EVENT_BUS.post(new FrameGraphSetupEvent(builder, targets, renderTargetDescriptor, frustum, camera, modelViewMatrix, projectionMatrix, deltaTracker, profiler)); } - @Nullable - private static Boolean enableStencil; - @ApiStatus.Internal - public static void configureMainRenderTarget() { - if (enableStencil != null) { - throw new IllegalStateException("configureMainRenderTarget() called more than once"); - } + public static MainTarget instantiateMainTarget(int width, int height) { var e = ModLoader.postEventWithReturn(new ConfigureMainRenderTargetEvent()); - enableStencil = e.isStencilEnabled(); - } - - public static boolean isStencilEnabled() { - if (enableStencil == null) { - throw new IllegalStateException("isStencilEnabled() called before ConfigureMainRenderTargetEvent was dispatched"); - } - return enableStencil; + return new MainTarget(width, height, e.isStencilEnabled()); } /**