-
-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a ConfigureMainRenderTargetEvent for enabling stenciling, simplified(?) #1830
Draft
Technici4n
wants to merge
18
commits into
neoforged:1.21.x
Choose a base branch
from
Technici4n:feature/depth-stenciling
base: 1.21.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ea3aeb4
Separate ClientModLoader begin/finish into two methods, implement Mai…
FiniteReality 5135ec1
Flow stencil information through post chains
FiniteReality 613cb46
Fix copy-paste fail in RenderTarget
FiniteReality f938bfe
Use an enum for the buffer type instead of two bools
FiniteReality b5cd630
Fix import whoopsie
FiniteReality 922fad6
Fix stenciling on the main target
FiniteReality 721d79d
Remove no-op method for enabling depth, pass desired width/height
FiniteReality ff68a10
Minimise patch by removing unnecessary indentation
FiniteReality 117c7af
Respond to XFact's feedback
FiniteReality f278e8d
Remove stencil-only patches in MainTarget
FiniteReality 552d636
Stencil requires depth
Technici4n 2278de1
Remove unnecessary AttachmentState patch
Technici4n fa4ebe7
Keep simplifying
Technici4n f3df3e4
Keep simplifying?
Technici4n 46e68fe
Missed old patch to delete
Technici4n 02a1ea2
getDescriptor simplification
Technici4n f62e417
Simplified event
shartte 3f5ddc1
Simplify allocation (+ missed genPatches)
Technici4n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- 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<T> getDescriptor() { | ||
+ return this.holder.getDescriptor(); | ||
+ } | ||
} | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
@@ -265,6 +_,11 @@ | ||
this.physicalResource = null; | ||
} | ||
} | ||
+ | ||
+ @Override | ||
+ public ResourceDescriptor<T> getDescriptor() { | ||
+ return descriptor; | ||
+ } | ||
} | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
@@ -363,6 +_,11 @@ | ||
@Override | ||
public String toString() { | ||
return this.name; | ||
+ } | ||
+ | ||
+ @Nullable | ||
+ public ResourceDescriptor<T> getDescriptor() { | ||
+ return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- a/com/mojang/blaze3d/pipeline/MainTarget.java | ||
+++ b/com/mojang/blaze3d/pipeline/MainTarget.java | ||
@@ -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.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) { | ||
+ super(e.useDepth(), e.useStencil()); | ||
+ this.createFrameBuffer(e.width(), e.height()); | ||
} | ||
|
||
private void createFrameBuffer(int p_166142_, int p_166143_) { | ||
@@ -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._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; | ||
@@ -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(); | ||
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); | ||
+ } else { | ||
+ GlStateManager._texImage2D(3553, 0, 6402, p_166145_.width, p_166145_.height, 0, 6402, 5126, null); | ||
+ } | ||
return GlStateManager._getError() != 1285; | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
patches/com/mojang/blaze3d/pipeline/RenderTarget.java.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- a/com/mojang/blaze3d/pipeline/RenderTarget.java | ||
+++ b/com/mojang/blaze3d/pipeline/RenderTarget.java | ||
@@ -25,6 +_,7 @@ | ||
public int viewWidth; | ||
public int viewHeight; | ||
public final boolean useDepth; | ||
+ public final boolean useStencil; | ||
public int frameBufferId; | ||
protected int colorTextureId; | ||
protected int depthBufferId; | ||
@@ -32,7 +_,15 @@ | ||
public int filterMode; | ||
|
||
public RenderTarget(boolean p_166199_) { | ||
- this.useDepth = p_166199_; | ||
+ this(p_166199_, false); | ||
+ } | ||
+ | ||
+ 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; | ||
@@ -96,7 +_,20 @@ | ||
GlStateManager._texParameter(3553, 34892, 0); | ||
GlStateManager._texParameter(3553, 10242, 33071); | ||
GlStateManager._texParameter(3553, 10243, 33071); | ||
- 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 { | ||
+ // 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); | ||
+ } | ||
} | ||
|
||
this.setFilterMode(9728, true); | ||
@@ -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.depthBufferId, | ||
+ 0); | ||
+ } | ||
|
||
this.checkStatus(); | ||
this.clear(); | ||
@@ -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); |
14 changes: 14 additions & 0 deletions
14
patches/com/mojang/blaze3d/pipeline/TextureTarget.java.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_); | ||
} |
18 changes: 18 additions & 0 deletions
18
patches/com/mojang/blaze3d/resource/RenderTargetDescriptor.java.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RenderTarget> { | ||
+public record RenderTargetDescriptor(int width, int height, boolean useDepth, boolean useStencil) implements ResourceDescriptor<RenderTarget> { | ||
+ 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_) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
patches/net/minecraft/client/renderer/LevelRenderer.java.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
patches/net/minecraft/client/renderer/PostChain.java.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- 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<ResourceLocation, ResourceHandle<RenderTarget>> map = new HashMap<>(this.internalTargets.size() + this.externalTargets.size()); | ||
|
||
+ // Enable the depth and stencil buffers based on whether any external targets use them. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what this patch is trying to achieve. |
||
+ // 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<ResourceLocation, PostChainConfig.InternalTarget> 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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/net/neoforged/neoforge/client/event/ConfigureMainRenderTargetEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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 net.neoforged.bus.api.Event; | ||
import net.neoforged.bus.api.ICancellableEvent; | ||
import net.neoforged.fml.LogicalSide; | ||
import net.neoforged.fml.event.IModBusEvent; | ||
|
||
/** | ||
* Fired when configuring the {@linkplain MainTarget main render target} during startup. | ||
* <p> | ||
* This event is not {@linkplain ICancellableEvent cancellable}. | ||
* <p> | ||
* 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; | ||
|
||
/** | ||
* Returns whether enabling the stencil buffer on the main render target was requested. | ||
* | ||
* @return <code>true</code>, if the stencil buffer is enabled, or <code>false</code> otherwise. | ||
*/ | ||
public boolean useStencil() { | ||
return this.useStencil; | ||
} | ||
|
||
/** | ||
* Enable the stencil buffer for the main render target. | ||
* | ||
* @return <code>this</code>, for method chaining. | ||
*/ | ||
public ConfigureMainRenderTargetEvent enableStencil() { | ||
this.useStencil = true; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this?