PostPass .java

net.minecraft.client.renderer.PostPass (class): members: UBO_SIZE_PER_SAMPLER, customUniforms, outputTargetId, uniformGroups, InputTexture, infoUbo, Input.

net.minecraft.client.rendererclass213 linesclient7.7 KB

Implements

API Surface (6)

void addToFrame(final FrameGraphBuilder frame, final Map<Identifier, ResourceHandle<RenderTarget>> targets, final GpuBufferSlice shaderOrthoMatrix)
void close()
void addToPass(final FramePass pass, final Map<Identifier, ResourceHandle<RenderTarget>> targets)
GpuTextureView texture(final Map<Identifier, ResourceHandle<RenderTarget>> targets)
void addToPass(final FramePass pass, final Map<Identifier, ResourceHandle<RenderTarget>> targets)
GpuTextureView texture(final Map<Identifier, ResourceHandle<RenderTarget>> targets)

Fields & Constants (8)

UBO_SIZE_PER_SAMPLER
name
pipeline
outputTargetId
customUniforms
infoUbo
inputs
uniformGroups

String literals (9)

" SamplerInfo"
"Missing handle for target "
"Post pass "
"SamplerInfo"
"Sampler"
"Missing "
"depth"
"color"
"texture for target "

Full Source

PostPass.java213 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer;
2
3
import com.mojang.blaze3d.ProjectionType;
4
import com.mojang.blaze3d.buffers.Std140Builder;
5
import com.mojang.blaze3d.buffers.Std140SizeCalculator;
6
import com.mojang.blaze3d.framegraph.FrameGraphBuilder;
7
import com.mojang.blaze3d.framegraph.FramePass;
8
import com.mojang.blaze3d.pipeline.RenderTarget;
9
import com.mojang.blaze3d.resource.ResourceHandle;
10
import com.mojang.blaze3d.systems.RenderSystem;
11
import com.mojang.blaze3d.systems.SamplerCache;
12
import com.mojang.renderpearl.api.buffers.GpuBuffer;
13
import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
14
import com.mojang.renderpearl.api.commands.CommandEncoder;
15
import com.mojang.renderpearl.api.commands.RenderPass;
16
import com.mojang.renderpearl.api.pipeline.RenderPipeline;
17
import com.mojang.renderpearl.api.textures.FilterMode;
18
import com.mojang.renderpearl.api.textures.GpuSampler;
19
import com.mojang.renderpearl.api.textures.GpuTextureView;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Optional;
24
import java.util.OptionalDouble;
25
import java.util.Map.Entry;
26
import net.fabricmc.api.EnvType;
27
import net.fabricmc.api.Environment;
28
import net.minecraft.client.renderer.texture.AbstractTexture;
29
import net.minecraft.resources.Identifier;
30
import org.lwjgl.system.MemoryStack;
31
32
@Environment(EnvType.CLIENT)
33
public class PostPass implements AutoCloseable {
34
	private static final int UBO_SIZE_PER_SAMPLER = new Std140SizeCalculator().putVec2().get();
35
	private final String name;
36
	private final RenderPipeline pipeline;
37
	private final Identifier outputTargetId;
38
	private final Map<String, GpuBuffer> customUniforms = new HashMap<>();
39
	private final MappableRingBuffer infoUbo;
40
	private final List<PostPass.Input> inputs;
41
42
	public PostPass(
43
		final RenderPipeline pipeline, final Identifier outputTargetId, final Map<String, List<UniformValue>> uniformGroups, final List<PostPass.Input> inputs
44
	) {
45
		this.pipeline = pipeline;
46
		this.name = pipeline.getLocation().toString();
47
		this.outputTargetId = outputTargetId;
48
		this.inputs = inputs;
49
50
		for (Entry<String, List<UniformValue>> uniformGroup : uniformGroups.entrySet()) {
51
			List<UniformValue> uniforms = uniformGroup.getValue();
52
			if (!uniforms.isEmpty()) {
53
				Std140SizeCalculator calculator = new Std140SizeCalculator();
54
55
				for (UniformValue uniform : uniforms) {
56
					uniform.addSize(calculator);
57
				}
58
59
				int size = calculator.get();
60
61
				try (MemoryStack stack = MemoryStack.stackPush()) {
62
					Std140Builder builder = Std140Builder.onStack(stack, size);
63
64
					for (UniformValue uniform : uniforms) {
65
						uniform.writeTo(builder);
66
					}
67
68
					this.customUniforms.put(uniformGroup.getKey(), RenderSystem.getDevice().createBuffer(() -> this.name + " / " + uniformGroup.getKey(), 128, builder.get()));
69
				}
70
			}
71
		}
72
73
		this.infoUbo = new MappableRingBuffer(() -> this.name + " SamplerInfo", 130, (inputs.size() + 1) * UBO_SIZE_PER_SAMPLER);
74
	}
75
76
	public void addToFrame(final FrameGraphBuilder frame, final Map<Identifier, ResourceHandle<RenderTarget>> targets, final GpuBufferSlice shaderOrthoMatrix) {
77
		FramePass pass = frame.addPass(this.name);
78
79
		for (PostPass.Input input : this.inputs) {
80
			input.addToPass(pass, targets);
81
		}
82
83
		ResourceHandle<RenderTarget> outputHandle = targets.computeIfPresent(
84
			this.outputTargetId, (id, handle) -> pass.readsAndWrites((ResourceHandle<RenderTarget>)handle)
85
		);
86
		if (outputHandle == null) {
87
			throw new IllegalStateException("Missing handle for target " + this.outputTargetId);
88
		}
89
90
		pass.executes(
91
			() -> {
92
				RenderTarget outputTarget = outputHandle.get();
93
				RenderSystem.backupProjectionMatrix();
94
				RenderSystem.setProjectionMatrix(shaderOrthoMatrix, ProjectionType.ORTHOGRAPHIC);
95
				CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder();
96
				SamplerCache samplerCache = RenderSystem.getSamplerCache();
97
				List<PostPass.InputTexture> inputTextures = this.inputs
98
					.stream()
99
					.map(
100
						i -> new PostPass.InputTexture(i.samplerName(), i.texture(targets), samplerCache.getClampToEdge(i.bilinear() ? FilterMode.LINEAR : FilterMode.NEAREST))
101
					)
102
					.toList();
103
104
				try (GpuBufferSlice.MappedView view = this.infoUbo.currentBuffer().map(false, true)) {
105
					Std140Builder builder = Std140Builder.intoBuffer(view.data());
106
					builder.putVec2(outputTarget.width, outputTarget.height);
107
108
					for (PostPass.InputTexture inputxxx : inputTextures) {
109
						builder.putVec2(inputxxx.view.getWidth(0), inputxxx.view.getHeight(0));
110
					}
111
				}
112
113
				try (RenderPass renderPass = commandEncoder.createRenderPass(
114
						() -> "Post pass " + this.name,
115
						outputTarget.getColorTextureView(),
116
						Optional.empty(),
117
						outputTarget.hasDepth() ? outputTarget.getDepthTextureView() : null,
118
						OptionalDouble.empty()
119
					)) {
120
					renderPass.setPipeline(RenderSystem.getCompiledPipeline(this.pipeline));
121
					RenderSystem.bindDefaultUniforms(renderPass);
122
					renderPass.setUniform("SamplerInfo", this.infoUbo.currentBuffer());
123
124
					for (Entry<String, GpuBuffer> entry : this.customUniforms.entrySet()) {
125
						renderPass.setUniform(entry.getKey(), entry.getValue());
126
					}
127
128
					for (PostPass.InputTexture inputx : inputTextures) {
129
						renderPass.setUniform(inputx.samplerName() + "Sampler", inputx.view(), inputx.sampler());
130
					}
131
132
					renderPass.draw(3, 1, 0, 0);
133
				}
134
135
				this.infoUbo.rotate();
136
				RenderSystem.restoreProjectionMatrix();
137
138
				for (PostPass.Input inputxx : this.inputs) {
139
					inputxx.cleanup(targets);
140
				}
141
			}
142
		);
143
	}
144
145
	@Override
146
	public void close() {
147
		for (GpuBuffer buffer : this.customUniforms.values()) {
148
			buffer.close();
149
		}
150
151
		this.infoUbo.close();
152
	}
153
154
	@Environment(EnvType.CLIENT)
155
	public interface Input {
156
		void addToPass(FramePass pass, Map<Identifier, ResourceHandle<RenderTarget>> targets);
157
158
		default void cleanup(final Map<Identifier, ResourceHandle<RenderTarget>> targets) {
159
		}
160
161
		GpuTextureView texture(final Map<Identifier, ResourceHandle<RenderTarget>> targets);
162
163
		String samplerName();
164
165
		boolean bilinear();
166
	}
167
168
	@Environment(EnvType.CLIENT)
169
	private record InputTexture(String samplerName, GpuTextureView view, GpuSampler sampler) {
170
	}
171
172
	@Environment(EnvType.CLIENT)
173
	public record TargetInput(String samplerName, Identifier targetId, boolean depthBuffer, boolean bilinear) implements PostPass.Input {
174
		private ResourceHandle<RenderTarget> getHandle(final Map<Identifier, ResourceHandle<RenderTarget>> targets) {
175
			ResourceHandle<RenderTarget> handle = targets.get(this.targetId);
176
			if (handle == null) {
177
				throw new IllegalStateException("Missing handle for target " + this.targetId);
178
			} else {
179
				return handle;
180
			}
181
		}
182
183
		@Override
184
		public void addToPass(final FramePass pass, final Map<Identifier, ResourceHandle<RenderTarget>> targets) {
185
			pass.reads(this.getHandle(targets));
186
		}
187
188
		@Override
189
		public GpuTextureView texture(final Map<Identifier, ResourceHandle<RenderTarget>> targets) {
190
			ResourceHandle<RenderTarget> handle = this.getHandle(targets);
191
			RenderTarget target = handle.get();
192
			GpuTextureView textureView = this.depthBuffer ? target.getDepthTextureView() : target.getColorTextureView();
193
			if (textureView == null) {
194
				throw new IllegalStateException("Missing " + (this.depthBuffer ? "depth" : "color") + "texture for target " + this.targetId);
195
			} else {
196
				return textureView;
197
			}
198
		}
199
	}
200
201
	@Environment(EnvType.CLIENT)
202
	public record TextureInput(String samplerName, AbstractTexture texture, int width, int height, boolean bilinear) implements PostPass.Input {
203
		@Override
204
		public void addToPass(final FramePass pass, final Map<Identifier, ResourceHandle<RenderTarget>> targets) {
205
		}
206
207
		@Override
208
		public GpuTextureView texture(final Map<Identifier, ResourceHandle<RenderTarget>> targets) {
209
			return this.texture.getTextureView();
210
		}
211
	}
212
}
213

Decompiled Minecraft 26.3 source of PostPass (net/minecraft/client/renderer/PostPass.java) with Mojang official mappings, stored verbatim on the site.