| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectArraySet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.function.Function;
|
| 13 | import java.util.stream.Stream;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import net.minecraft.util.ExtraCodecs;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public record PostChainConfig(Map<Identifier, PostChainConfig.InternalTarget> internalTargets, List<PostChainConfig.Pass> passes) {
|
| 21 | public static final Codec<PostChainConfig> CODEC = RecordCodecBuilder.create(
|
| 22 | i -> i.group(
|
| 23 | Codec.unboundedMap(Identifier.CODEC, PostChainConfig.InternalTarget.CODEC).optionalFieldOf("targets", Map.of()).forGetter(PostChainConfig::internalTargets),
|
| 24 | PostChainConfig.Pass.CODEC.listOf().optionalFieldOf("passes", List.of()).forGetter(PostChainConfig::passes)
|
| 25 | )
|
| 26 | .apply(i, PostChainConfig::new)
|
| 27 | );
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public sealed interface Input permits PostChainConfig.TargetInput, PostChainConfig.TextureInput {
|
| 31 | Codec<PostChainConfig.Input> CODEC = Codec.xor(PostChainConfig.TextureInput.CODEC, PostChainConfig.TargetInput.CODEC)
|
| 32 | .xmap(either -> either.map(Function.identity(), Function.identity()), input -> {
|
| 33 | return switch (input) {
|
| 34 | case PostChainConfig.TextureInput texture -> Either.left(texture);
|
| 35 | case PostChainConfig.TargetInput target -> Either.right(target);
|
| 36 | default -> throw new MatchException(null, null);
|
| 37 | };
|
| 38 | });
|
| 39 |
|
| 40 | String samplerName();
|
| 41 |
|
| 42 | Set<Identifier> referencedTargets();
|
| 43 | }
|
| 44 |
|
| 45 | @Environment(EnvType.CLIENT)
|
| 46 | public record InternalTarget(Optional<Integer> width, Optional<Integer> height, boolean persistent, int clearColor) {
|
| 47 | public static final Codec<PostChainConfig.InternalTarget> CODEC = RecordCodecBuilder.create(
|
| 48 | i -> i.group(
|
| 49 | ExtraCodecs.POSITIVE_INT.optionalFieldOf("width").forGetter(PostChainConfig.InternalTarget::width),
|
| 50 | ExtraCodecs.POSITIVE_INT.optionalFieldOf("height").forGetter(PostChainConfig.InternalTarget::height),
|
| 51 | Codec.BOOL.optionalFieldOf("persistent", false).forGetter(PostChainConfig.InternalTarget::persistent),
|
| 52 | ExtraCodecs.ARGB_COLOR_CODEC.optionalFieldOf("clear_color", 0).forGetter(PostChainConfig.InternalTarget::clearColor)
|
| 53 | )
|
| 54 | .apply(i, PostChainConfig.InternalTarget::new)
|
| 55 | );
|
| 56 | }
|
| 57 |
|
| 58 | @Environment(EnvType.CLIENT)
|
| 59 | public record Pass(
|
| 60 | Identifier vertexShaderId, Identifier fragmentShaderId, List<PostChainConfig.Input> inputs, Identifier outputTarget, Map<String, List<UniformValue>> uniforms
|
| 61 | ) {
|
| 62 | private static final Codec<List<PostChainConfig.Input>> INPUTS_CODEC = PostChainConfig.Input.CODEC.listOf().validate(inputs -> {
|
| 63 | Set<String> samplerName = new ObjectArraySet<>(inputs.size());
|
| 64 |
|
| 65 | for (PostChainConfig.Input input : inputs) {
|
| 66 | if (!samplerName.add(input.samplerName())) {
|
| 67 | return DataResult.error(() -> "Encountered repeated sampler name: " + input.samplerName());
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | return DataResult.success(inputs);
|
| 72 | });
|
| 73 | private static final Codec<Map<String, List<UniformValue>>> UNIFORM_BLOCKS_CODEC = Codec.unboundedMap(Codec.STRING, UniformValue.CODEC.listOf());
|
| 74 | public static final Codec<PostChainConfig.Pass> CODEC = RecordCodecBuilder.create(
|
| 75 | i -> i.group(
|
| 76 | Identifier.CODEC.fieldOf("vertex_shader").forGetter(PostChainConfig.Pass::vertexShaderId),
|
| 77 | Identifier.CODEC.fieldOf("fragment_shader").forGetter(PostChainConfig.Pass::fragmentShaderId),
|
| 78 | INPUTS_CODEC.optionalFieldOf("inputs", List.of()).forGetter(PostChainConfig.Pass::inputs),
|
| 79 | Identifier.CODEC.fieldOf("output").forGetter(PostChainConfig.Pass::outputTarget),
|
| 80 | UNIFORM_BLOCKS_CODEC.optionalFieldOf("uniforms", Map.of()).forGetter(PostChainConfig.Pass::uniforms)
|
| 81 | )
|
| 82 | .apply(i, PostChainConfig.Pass::new)
|
| 83 | );
|
| 84 |
|
| 85 | public Stream<Identifier> referencedTargets() {
|
| 86 | Stream<Identifier> inputTargets = this.inputs.stream().flatMap(input -> input.referencedTargets().stream());
|
| 87 | return Stream.concat(inputTargets, Stream.of(this.outputTarget));
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Environment(EnvType.CLIENT)
|
| 92 | public record TargetInput(String samplerName, Identifier targetId, boolean useDepthBuffer, boolean bilinear) implements PostChainConfig.Input {
|
| 93 | public static final Codec<PostChainConfig.TargetInput> CODEC = RecordCodecBuilder.create(
|
| 94 | i -> i.group(
|
| 95 | Codec.STRING.fieldOf("sampler_name").forGetter(PostChainConfig.TargetInput::samplerName),
|
| 96 | Identifier.CODEC.fieldOf("target").forGetter(PostChainConfig.TargetInput::targetId),
|
| 97 | Codec.BOOL.optionalFieldOf("use_depth_buffer", false).forGetter(PostChainConfig.TargetInput::useDepthBuffer),
|
| 98 | Codec.BOOL.optionalFieldOf("bilinear", false).forGetter(PostChainConfig.TargetInput::bilinear)
|
| 99 | )
|
| 100 | .apply(i, PostChainConfig.TargetInput::new)
|
| 101 | );
|
| 102 |
|
| 103 | @Override
|
| 104 | public Set<Identifier> referencedTargets() {
|
| 105 | return Set.of(this.targetId);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | @Environment(EnvType.CLIENT)
|
| 110 | public record TextureInput(String samplerName, Identifier location, int width, int height, boolean bilinear) implements PostChainConfig.Input {
|
| 111 | public static final Codec<PostChainConfig.TextureInput> CODEC = RecordCodecBuilder.create(
|
| 112 | i -> i.group(
|
| 113 | Codec.STRING.fieldOf("sampler_name").forGetter(PostChainConfig.TextureInput::samplerName),
|
| 114 | Identifier.CODEC.fieldOf("location").forGetter(PostChainConfig.TextureInput::location),
|
| 115 | ExtraCodecs.POSITIVE_INT.fieldOf("width").forGetter(PostChainConfig.TextureInput::width),
|
| 116 | ExtraCodecs.POSITIVE_INT.fieldOf("height").forGetter(PostChainConfig.TextureInput::height),
|
| 117 | Codec.BOOL.optionalFieldOf("bilinear", false).forGetter(PostChainConfig.TextureInput::bilinear)
|
| 118 | )
|
| 119 | .apply(i, PostChainConfig.TextureInput::new)
|
| 120 | );
|
| 121 |
|
| 122 | @Override
|
| 123 | public Set<Identifier> referencedTargets() {
|
| 124 | return Set.of();
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|