164 lines
7.3 KiB
Java
164 lines
7.3 KiB
Java
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 com.mojang.blaze3d.vertex.DefaultVertexFormat;
|
|
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.PostChainConfig.FixedSizedTarget;
|
|
import net.minecraft.client.renderer.PostChainConfig.FullScreenTarget;
|
|
import net.minecraft.client.renderer.PostChainConfig.Input;
|
|
import net.minecraft.client.renderer.PostChainConfig.InternalTarget;
|
|
import net.minecraft.client.renderer.PostChainConfig.Pass;
|
|
import net.minecraft.client.renderer.PostChainConfig.TargetInput;
|
|
import net.minecraft.client.renderer.PostChainConfig.TextureInput;
|
|
import net.minecraft.client.renderer.PostChainConfig.Uniform;
|
|
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<PostPass> passes;
|
|
private final Map<ResourceLocation, InternalTarget> internalTargets;
|
|
private final Set<ResourceLocation> externalTargets;
|
|
|
|
private PostChain(List<PostPass> passes, Map<ResourceLocation, InternalTarget> internalTargets, Set<ResourceLocation> externalTargets) {
|
|
this.passes = passes;
|
|
this.internalTargets = internalTargets;
|
|
this.externalTargets = externalTargets;
|
|
}
|
|
|
|
public static PostChain load(PostChainConfig config, TextureManager textureManager, ShaderManager shaderManager, Set<ResourceLocation> externalTargets) throws ShaderManager.CompilationException {
|
|
Stream<ResourceLocation> stream = config.passes().stream().flatMap(passx -> passx.inputs().stream()).flatMap(input -> input.referencedTargets().stream());
|
|
Set<ResourceLocation> set = (Set<ResourceLocation>)stream.filter(resourceLocation -> !config.internalTargets().containsKey(resourceLocation))
|
|
.collect(Collectors.toSet());
|
|
Set<ResourceLocation> set2 = Sets.<ResourceLocation>difference(set, externalTargets);
|
|
if (!set2.isEmpty()) {
|
|
throw new ShaderManager.CompilationException("Referenced external targets are not available in this context: " + set2);
|
|
} else {
|
|
Builder<PostPass> builder = ImmutableList.builder();
|
|
|
|
for (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, Pass pass) throws ShaderManager.CompilationException {
|
|
ResourceLocation resourceLocation = pass.program();
|
|
CompiledShaderProgram compiledShaderProgram = shaderManager.getProgramForLoading(
|
|
new ShaderProgram(resourceLocation, DefaultVertexFormat.POSITION, ShaderDefines.EMPTY)
|
|
);
|
|
|
|
for (Uniform uniform : pass.uniforms()) {
|
|
String string = uniform.name();
|
|
if (compiledShaderProgram.getUniform(string) == null) {
|
|
throw new ShaderManager.CompilationException("Uniform '" + string + "' does not exist for " + resourceLocation);
|
|
}
|
|
}
|
|
|
|
String string2 = resourceLocation.toString();
|
|
PostPass postPass = new PostPass(string2, compiledShaderProgram, pass.outputTarget(), pass.uniforms());
|
|
|
|
for (Input input : pass.inputs()) {
|
|
switch (input) {
|
|
case TextureInput(String var35, ResourceLocation var36, int var37, int var38, boolean var39):
|
|
AbstractTexture abstractTexture = textureManager.getTexture(var36.withPath((UnaryOperator<String>)(stringx -> "textures/effect/" + stringx + ".png")));
|
|
abstractTexture.setFilter(var39, false);
|
|
postPass.addInput(new PostPass.TextureInput(var35, abstractTexture, var37, var38));
|
|
break;
|
|
case TargetInput(String var22, ResourceLocation var41, boolean var42, boolean var43):
|
|
postPass.addInput(new PostPass.TargetInput(var22, var41, var42, var43));
|
|
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<ResourceLocation, ResourceHandle<RenderTarget>> map = new HashMap(this.internalTargets.size() + this.externalTargets.size());
|
|
|
|
for (ResourceLocation resourceLocation : this.externalTargets) {
|
|
map.put(resourceLocation, targetBundle.getOrThrow(resourceLocation));
|
|
}
|
|
|
|
for (Entry<ResourceLocation, InternalTarget> entry : this.internalTargets.entrySet()) {
|
|
ResourceLocation resourceLocation2 = (ResourceLocation)entry.getKey();
|
|
|
|
RenderTargetDescriptor renderTargetDescriptor = switch ((InternalTarget)entry.getValue()) {
|
|
case FixedSizedTarget(int var25, int var26) -> new RenderTargetDescriptor(var25, var26, true);
|
|
case 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<RenderTarget>)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<RenderTarget> handle) {
|
|
return new 1(handle, id);
|
|
}
|
|
|
|
void replace(ResourceLocation id, ResourceHandle<RenderTarget> handle);
|
|
|
|
@Nullable
|
|
ResourceHandle<RenderTarget> get(ResourceLocation id);
|
|
|
|
default ResourceHandle<RenderTarget> getOrThrow(ResourceLocation id) {
|
|
ResourceHandle<RenderTarget> resourceHandle = this.get(id);
|
|
if (resourceHandle == null) {
|
|
throw new IllegalArgumentException("Missing target with id " + id);
|
|
} else {
|
|
return resourceHandle;
|
|
}
|
|
}
|
|
}
|
|
}
|