PostChain .java

net.minecraft.client.renderer.PostChain (class, 230 lines): members: getReferencedExternalTargets, allowedExternalTargets, closePersistentTargets, passes.

net.minecraft.client.rendererclass230 linesclient9.1 KB

Implements

API Surface (7)

Identifier id()
PostChain load(final PostChainConfig config, final TextureManager textureManager, final Set<Identifier> allowedExternalTargets, final Identifier id, final Projection projection, final ProjectionMatrixBuffer projectionMatrixBuffer)
Set<Identifier> getReferencedExternalTargets(final PostChainConfig config)
void addToFrame(final FrameGraphBuilder frame, final int screenWidth, final int screenHeight, final PostChain.TargetBundle providedTargets)
void process(final RenderTarget mainTarget, final GraphicsResourceAllocator resourceAllocator)
void closePersistentTargets()
void close()

Fields & Constants (11)

MAIN_TARGET_ID
id
passes
internalTargets
externalTargets
persistentTargets
projection
projectionMatrixBuffer
config
textureManager
allowedExternalTargets

String literals (7)

"main"
"Sampler"
"SamplerInfo"
"Failed to compile post processing pipeline "
"textures/effect/"
".png"
"Missing target with id "

Full Source

PostChain.java230 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.Sets;
5
import com.google.common.collect.ImmutableList.Builder;
6
import com.mojang.blaze3d.framegraph.FrameGraphBuilder;
7
import com.mojang.blaze3d.pipeline.RenderTarget;
8
import com.mojang.blaze3d.resource.GraphicsResourceAllocator;
9
import com.mojang.blaze3d.resource.RenderTargetDescriptor;
10
import com.mojang.blaze3d.resource.ResourceHandle;
11
import com.mojang.blaze3d.systems.RenderSystem;
12
import com.mojang.renderpearl.api.GpuFormat;
13
import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
14
import com.mojang.renderpearl.api.pipeline.BindGroupLayout;
15
import com.mojang.renderpearl.api.pipeline.ColorTargetState;
16
import com.mojang.renderpearl.api.pipeline.RenderPipeline;
17
import com.mojang.renderpearl.api.pipeline.UniformType;
18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Set;
23
import java.util.Map.Entry;
24
import java.util.stream.Collectors;
25
import java.util.stream.Stream;
26
import net.fabricmc.api.EnvType;
27
import net.fabricmc.api.Environment;
28
import net.minecraft.client.renderer.PostChain.TargetBundle.1;
29
import net.minecraft.client.renderer.texture.AbstractTexture;
30
import net.minecraft.client.renderer.texture.TextureManager;
31
import net.minecraft.resources.Identifier;
32
import net.minecraft.util.ARGB;
33
import org.jspecify.annotations.Nullable;
34
35
@Environment(EnvType.CLIENT)
36
public class PostChain implements AutoCloseable {
37
	public static final Identifier MAIN_TARGET_ID = Identifier.withDefaultNamespace("main");
38
	private final Identifier id;
39
	private final List<PostPass> passes;
40
	private final Map<Identifier, PostChainConfig.InternalTarget> internalTargets;
41
	private final Set<Identifier> externalTargets;
42
	private final Map<Identifier, RenderTarget> persistentTargets = new HashMap<>();
43
	private final Projection projection;
44
	private final ProjectionMatrixBuffer projectionMatrixBuffer;
45
46
	private PostChain(
47
		final Identifier id,
48
		final List<PostPass> passes,
49
		final Map<Identifier, PostChainConfig.InternalTarget> internalTargets,
50
		final Set<Identifier> externalTargets,
51
		final Projection projection,
52
		final ProjectionMatrixBuffer projectionMatrixBuffer
53
	) {
54
		this.id = id;
55
		this.passes = passes;
56
		this.internalTargets = internalTargets;
57
		this.externalTargets = externalTargets;
58
		this.projection = projection;
59
		this.projectionMatrixBuffer = projectionMatrixBuffer;
60
	}
61
62
	public Identifier id() {
63
		return this.id;
64
	}
65
66
	public static PostChain load(
67
		final PostChainConfig config,
68
		final TextureManager textureManager,
69
		final Set<Identifier> allowedExternalTargets,
70
		final Identifier id,
71
		final Projection projection,
72
		final ProjectionMatrixBuffer projectionMatrixBuffer
73
	) throws ShaderManager.CompilationException {
74
		Set<Identifier> referencedExternalTargets = getReferencedExternalTargets(config);
75
		Set<Identifier> invalidExternalTargets = Sets.difference(referencedExternalTargets, allowedExternalTargets);
76
		if (!invalidExternalTargets.isEmpty()) {
77
			throw new ShaderManager.CompilationException("Referenced external targets are not available in this context: " + invalidExternalTargets);
78
		}
79
80
		Builder<PostPass> passes = ImmutableList.builder();
81
82
		for (int i = 0; i < config.passes().size(); i++) {
83
			PostChainConfig.Pass pass = config.passes().get(i);
84
			passes.add(createPass(textureManager, pass, id.withSuffix("/" + i)));
85
		}
86
87
		return new PostChain(id, passes.build(), config.internalTargets(), referencedExternalTargets, projection, projectionMatrixBuffer);
88
	}
89
90
	public static Set<Identifier> getReferencedExternalTargets(final PostChainConfig config) {
91
		Stream<Identifier> referencedTargets = config.passes().stream().flatMap(PostChainConfig.Pass::referencedTargets);
92
		return referencedTargets.filter(targetId -> !config.internalTargets().containsKey(targetId)).collect(Collectors.toSet());
93
	}
94
95
	private static PostPass createPass(final TextureManager textureManager, final PostChainConfig.Pass config, final Identifier id) throws ShaderManager.CompilationException {
96
		RenderPipeline.Builder pipelineBuilder = RenderPipeline.builder(RenderPipelines.POST_PROCESSING_SNIPPET)
97
			.withFragmentShader(config.fragmentShaderId())
98
			.withVertexShader(config.vertexShaderId())
99
			.withLocation(id);
100
		BindGroupLayout.Builder bindGroupLayoutBuilder = BindGroupLayout.builder();
101
102
		for (PostChainConfig.Input input : config.inputs()) {
103
			bindGroupLayoutBuilder.withUniform(input.samplerName() + "Sampler", UniformType.COMBINED_IMAGE_SAMPLER);
104
		}
105
106
		bindGroupLayoutBuilder.withUniform("SamplerInfo", UniformType.UNIFORM_BUFFER);
107
108
		for (String uniformGroupName : config.uniforms().keySet()) {
109
			bindGroupLayoutBuilder.withUniform(uniformGroupName, UniformType.UNIFORM_BUFFER);
110
		}
111
112
		pipelineBuilder.withBindGroupLayout(bindGroupLayoutBuilder.build());
113
		pipelineBuilder.withColorTargetState(ColorTargetState.DEFAULT);
114
		RenderPipeline pipeline = pipelineBuilder.build();
115
		if (RenderSystem.getCompiledPipelineNullable(pipeline) == null) {
116
			throw new ShaderManager.CompilationException("Failed to compile post processing pipeline " + pipeline.getLocation());
117
		}
118
119
		List<PostPass.Input> inputs = new ArrayList<>();
120
121
		for (PostChainConfig.Input input : config.inputs()) {
122
			switch (input) {
123
				case PostChainConfig.TextureInput(String samplerName, Identifier location, int width, int height, boolean bilinear):
124
					AbstractTexture var42 = textureManager.getTexture(location.withPath(path -> "textures/effect/" + path + ".png"));
125
					inputs.add(new PostPass.TextureInput(samplerName, var42, width, height, bilinear));
126
					break;
127
				case PostChainConfig.TargetInput(String samplerName, Identifier targetId, boolean useDepthBuffer, boolean bilinear):
128
					inputs.add(new PostPass.TargetInput(samplerName, targetId, useDepthBuffer, bilinear));
129
					break;
130
				default:
131
					throw new MatchException(null, null);
132
			}
133
		}
134
135
		return new PostPass(pipeline, config.outputTarget(), config.uniforms(), inputs);
136
	}
137
138
	public void addToFrame(final FrameGraphBuilder frame, final int screenWidth, final int screenHeight, final PostChain.TargetBundle providedTargets) {
139
		this.projection.setSize(screenWidth, screenHeight);
140
		GpuBufferSlice projectionBuffer = this.projectionMatrixBuffer.getBuffer(this.projection);
141
		Map<Identifier, ResourceHandle<RenderTarget>> targets = new HashMap<>(this.internalTargets.size() + this.externalTargets.size());
142
143
		for (Identifier id : this.externalTargets) {
144
			targets.put(id, providedTargets.getOrThrow(id));
145
		}
146
147
		for (Entry<Identifier, PostChainConfig.InternalTarget> entry : this.internalTargets.entrySet()) {
148
			Identifier id = entry.getKey();
149
			PostChainConfig.InternalTarget target = entry.getValue();
150
			RenderTargetDescriptor descriptor = new RenderTargetDescriptor(
151
				target.width().orElse(screenWidth),
152
				target.height().orElse(screenHeight),
153
				new RenderTargetDescriptor.TextureProperties(ARGB.vector4fFromARGB32(target.clearColor()), GpuFormat.RGBA8_UNORM),
154
				RenderTargetDescriptor.TextureProperties.DEFAULT_DEPTH
155
			);
156
			if (target.persistent()) {
157
				RenderTarget persistentTarget = this.getOrCreatePersistentTarget(id, descriptor);
158
				targets.put(id, frame.importExternal(id.toString(), persistentTarget));
159
			} else {
160
				targets.put(id, frame.createInternal(id.toString(), descriptor));
161
			}
162
		}
163
164
		for (PostPass pass : this.passes) {
165
			pass.addToFrame(frame, targets, projectionBuffer);
166
		}
167
168
		for (Identifier id : this.externalTargets) {
169
			providedTargets.replace(id, targets.get(id));
170
		}
171
	}
172
173
	@Deprecated
174
	public void process(final RenderTarget mainTarget, final GraphicsResourceAllocator resourceAllocator) {
175
		FrameGraphBuilder frame = new FrameGraphBuilder();
176
		PostChain.TargetBundle targets = PostChain.TargetBundle.of(MAIN_TARGET_ID, frame.importExternal("main", mainTarget));
177
		this.addToFrame(frame, mainTarget.width, mainTarget.height, targets);
178
		frame.execute(resourceAllocator);
179
	}
180
181
	private RenderTarget getOrCreatePersistentTarget(final Identifier id, final RenderTargetDescriptor descriptor) {
182
		RenderTarget target = this.persistentTargets.get(id);
183
		if (target == null || target.width != descriptor.width() || target.height != descriptor.height()) {
184
			if (target != null) {
185
				target.destroyBuffers();
186
			}
187
188
			target = descriptor.allocate();
189
			descriptor.prepare(target);
190
			this.persistentTargets.put(id, target);
191
		}
192
193
		return target;
194
	}
195
196
	public void closePersistentTargets() {
197
		this.persistentTargets.values().forEach(RenderTarget::destroyBuffers);
198
		this.persistentTargets.clear();
199
	}
200
201
	@Override
202
	public void close() {
203
		this.closePersistentTargets();
204
205
		for (PostPass pass : this.passes) {
206
			pass.close();
207
		}
208
	}
209
210
	@Environment(EnvType.CLIENT)
211
	public interface TargetBundle {
212
		static PostChain.TargetBundle of(final Identifier targetId, final ResourceHandle<RenderTarget> target) {
213
			return new 1(target, targetId);
214
		}
215
216
		void replace(Identifier id, ResourceHandle<RenderTarget> handle);
217
218
		@Nullable ResourceHandle<RenderTarget> get(Identifier id);
219
220
		default ResourceHandle<RenderTarget> getOrThrow(final Identifier id) {
221
			ResourceHandle<RenderTarget> handle = this.get(id);
222
			if (handle == null) {
223
				throw new IllegalArgumentException("Missing target with id " + id);
224
			} else {
225
				return handle;
226
			}
227
		}
228
	}
229
}
230

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