Skip to content

Commit

Permalink
Flow stencil information through post chains
Browse files Browse the repository at this point in the history
  • Loading branch information
FiniteReality committed Nov 30, 2024
1 parent 155b835 commit e354ca3
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 1 deletion.
46 changes: 46 additions & 0 deletions patches/com/mojang/blaze3d/framegraph/FrameGraphBuilder.java.patch
Original file line number Diff line number Diff line change
@@ -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<T> 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<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)
@@ -364,5 +_,8 @@
public String toString() {
return this.name;
}
+
+ @Nullable
+ public abstract ResourceDescriptor<T> getDescriptor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
return this.depthBufferId;
+ }
+
+ public int getStencilBufferId() {
+ public int getStencilTextureId() {
+ return this.stencilBufferId;
}
}
14 changes: 14 additions & 0 deletions patches/com/mojang/blaze3d/pipeline/TextureTarget.java.patch
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_);
}
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_) {
10 changes: 10 additions & 0 deletions patches/net/minecraft/client/renderer/LevelRenderer.java.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
--- a/net/minecraft/client/renderer/LevelRenderer.java
+++ b/net/minecraft/client/renderer/LevelRenderer.java
@@ -464,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);
@@ -485,7 +_,7 @@
RenderSystem.clear(16640);
});
Expand Down
48 changes: 48 additions & 0 deletions patches/net/minecraft/client/renderer/PostChain.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--- a/net/minecraft/client/renderer/PostChain.java
+++ b/net/minecraft/client/renderer/PostChain.java
@@ -83,8 +_,8 @@
abstracttexture.setFilter(flag, false);
postpass.addInput(new PostPass.TextureInput(s3, abstracttexture, i, j));
continue;
- case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation2, boolean flag1, boolean flag2):
- postpass.addInput(new PostPass.TargetInput(s1, resourcelocation2, flag1, flag2));
+ case PostChainConfig.TargetInput(String s1, ResourceLocation resourcelocation2, boolean flag1, boolean flag2, boolean useStencilBuffer):
+ postpass.addInput(new PostPass.TargetInput(s1, resourcelocation2, flag1, flag2, useStencilBuffer));
continue;
default:
throw new MatchException(null, null);
@@ -99,17 +_,32 @@
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.
+ // 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<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));
23 changes: 23 additions & 0 deletions patches/net/minecraft/client/renderer/PostChainConfig.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--- a/net/minecraft/client/renderer/PostChainConfig.java
+++ b/net/minecraft/client/renderer/PostChainConfig.java
@@ -103,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<PostChainConfig.TargetInput> 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)
);
28 changes: 28 additions & 0 deletions patches/net/minecraft/client/renderer/PostPass.java.patch
Original file line number Diff line number Diff line change
@@ -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<RenderTarget> getHandle(Map<ResourceLocation, ResourceHandle<RenderTarget>> p_364534_) {
ResourceHandle<RenderTarget> resourcehandle = p_364534_.get(this.targetId);
if (resourcehandle == null) {
@@ -142,7 +_,12 @@
ResourceHandle<RenderTarget> 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);
}

4 changes: 4 additions & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
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
Expand Down

0 comments on commit e354ca3

Please sign in to comment.