package net.minecraft.client.renderer; import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import com.google.common.collect.ImmutableList.Builder; import com.mojang.blaze3d.framegraph.FrameGraphBuilder; import com.mojang.blaze3d.pipeline.RenderTarget; import com.mojang.blaze3d.resource.GraphicsResourceAllocator; import com.mojang.blaze3d.resource.RenderTargetDescriptor; import com.mojang.blaze3d.resource.ResourceHandle; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import java.util.function.UnaryOperator; import java.util.stream.Collectors; import java.util.stream.Stream; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.renderer.PostChain.TargetBundle.1; import net.minecraft.client.renderer.PostPass.TargetInput; import net.minecraft.client.renderer.PostPass.TextureInput; import net.minecraft.client.renderer.texture.AbstractTexture; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.Nullable; import org.joml.Matrix4f; @Environment(EnvType.CLIENT) public class PostChain { public static final ResourceLocation MAIN_TARGET_ID = ResourceLocation.withDefaultNamespace("main"); private final List passes; private final Map internalTargets; private final Set externalTargets; private PostChain(List passes, Map internalTargets, Set externalTargets) { this.passes = passes; this.internalTargets = internalTargets; this.externalTargets = externalTargets; } public static PostChain load(PostChainConfig config, TextureManager textureManager, ShaderManager shaderManager, Set externalTargets) throws ShaderManager.CompilationException { Stream stream = config.passes().stream().flatMap(passx -> passx.inputs().stream()).flatMap(input -> input.referencedTargets().stream()); Set set = (Set)stream.filter(resourceLocation -> !config.internalTargets().containsKey(resourceLocation)) .collect(Collectors.toSet()); Set set2 = Sets.difference(set, externalTargets); if (!set2.isEmpty()) { throw new ShaderManager.CompilationException("Referenced external targets are not available in this context: " + set2); } else { Builder builder = ImmutableList.builder(); for (PostChainConfig.Pass pass : config.passes()) { builder.add(createPass(textureManager, shaderManager, pass)); } return new PostChain(builder.build(), config.internalTargets(), set); } } private static PostPass createPass(TextureManager textureManager, ShaderManager shaderManager, PostChainConfig.Pass pass) throws ShaderManager.CompilationException { CompiledShaderProgram compiledShaderProgram = shaderManager.getProgramForLoading(pass.program()); for (PostChainConfig.Uniform uniform : pass.uniforms()) { String string = uniform.name(); if (compiledShaderProgram.getUniform(string) == null) { throw new ShaderManager.CompilationException("Uniform '" + string + "' does not exist for " + pass.programId()); } } String string2 = pass.programId().toString(); PostPass postPass = new PostPass(string2, compiledShaderProgram, pass.outputTarget(), pass.uniforms()); for (PostChainConfig.Input input : pass.inputs()) { switch (input) { case PostChainConfig.TextureInput(String var34, ResourceLocation var35, int var36, int var37, boolean var38): AbstractTexture abstractTexture = textureManager.getTexture(var35.withPath((UnaryOperator)(stringx -> "textures/effect/" + stringx + ".png"))); abstractTexture.setFilter(var38, false); postPass.addInput(new TextureInput(var34, abstractTexture, var36, var37)); break; case PostChainConfig.TargetInput(String var21, ResourceLocation var40, boolean var41, boolean var42): postPass.addInput(new TargetInput(var21, var40, var41, var42)); break; default: throw new MatchException(null, null); } } return postPass; } public void addToFrame(FrameGraphBuilder frameGraphBuilder, int width, int height, PostChain.TargetBundle targetBundle) { Matrix4f matrix4f = new Matrix4f().setOrtho(0.0F, width, 0.0F, height, 0.1F, 1000.0F); Map> map = new HashMap(this.internalTargets.size() + this.externalTargets.size()); for (ResourceLocation resourceLocation : this.externalTargets) { map.put(resourceLocation, targetBundle.getOrThrow(resourceLocation)); } for (Entry entry : this.internalTargets.entrySet()) { ResourceLocation resourceLocation2 = (ResourceLocation)entry.getKey(); RenderTargetDescriptor renderTargetDescriptor = switch ((PostChainConfig.InternalTarget)entry.getValue()) { case PostChainConfig.FixedSizedTarget(int var25, int var26) -> new RenderTargetDescriptor(var25, var26, true); case PostChainConfig.FullScreenTarget var16 -> new RenderTargetDescriptor(width, height, true); default -> throw new MatchException(null, null); }; map.put(resourceLocation2, frameGraphBuilder.createInternal(resourceLocation2.toString(), renderTargetDescriptor)); } for (PostPass postPass : this.passes) { postPass.addToFrame(frameGraphBuilder, map, matrix4f); } for (ResourceLocation resourceLocation : this.externalTargets) { targetBundle.replace(resourceLocation, (ResourceHandle)map.get(resourceLocation)); } } @Deprecated public void process(RenderTarget target, GraphicsResourceAllocator graphicsResourceAllocator) { FrameGraphBuilder frameGraphBuilder = new FrameGraphBuilder(); PostChain.TargetBundle targetBundle = PostChain.TargetBundle.of(MAIN_TARGET_ID, frameGraphBuilder.importExternal("main", target)); this.addToFrame(frameGraphBuilder, target.width, target.height, targetBundle); frameGraphBuilder.execute(graphicsResourceAllocator); } public void setUniform(String name, float backgroundBlurriness) { for (PostPass postPass : this.passes) { postPass.getShader().safeGetUniform(name).set(backgroundBlurriness); } } @Environment(EnvType.CLIENT) public interface TargetBundle { static PostChain.TargetBundle of(ResourceLocation id, ResourceHandle handle) { return new 1(handle, id); } void replace(ResourceLocation id, ResourceHandle handle); @Nullable ResourceHandle get(ResourceLocation id); default ResourceHandle getOrThrow(ResourceLocation id) { ResourceHandle resourceHandle = this.get(id); if (resourceHandle == null) { throw new IllegalArgumentException("Missing target with id " + id); } else { return resourceHandle; } } } }