| 1 | package net.minecraft.client.renderer.feature.phase;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.client.renderer.feature.FeatureRendererType;
|
| 14 | import net.minecraft.client.renderer.feature.submit.BatchableSubmit;
|
| 15 | import net.minecraft.client.renderer.feature.submit.SubmitNode;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import org.apache.commons.lang3.ArrayUtils;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class SimpleFeatureRenderPhase implements FeatureRenderPhase<SubmitNode> {
|
| 22 | private SimpleFeatureRenderPhase.@Nullable FeatureSubmits<?>[] submitsByFeature = new SimpleFeatureRenderPhase.FeatureSubmits[0];
|
| 23 |
|
| 24 | @Override
|
| 25 | public void submit(final SubmitNode submit) {
|
| 26 | FeatureRendererType<? extends SubmitNode> type = submit.featureType();
|
| 27 | if (this.submitsByFeature.length <= type.id()) {
|
| 28 | this.submitsByFeature = Arrays.copyOf(this.submitsByFeature, Mth.roundToward(type.id() + 1, 16));
|
| 29 | }
|
| 30 |
|
| 31 | SimpleFeatureRenderPhase.FeatureSubmits<?> submits = this.submitsByFeature[type.id()];
|
| 32 | if (submits == null) {
|
| 33 | submits = new SimpleFeatureRenderPhase.FeatureSubmits<>(type);
|
| 34 | this.submitsByFeature[type.id()] = submits;
|
| 35 | }
|
| 36 |
|
| 37 | submits.addUnchecked(submit);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public void sortInto(final FeatureRenderPhase.Output output) {
|
| 42 | for (SimpleFeatureRenderPhase.FeatureSubmits<?> submits : maybeShuffle(this.submitsByFeature)) {
|
| 43 | if (submits != null) {
|
| 44 | sortFeatureInto(output, submits);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | this.clear();
|
| 49 | }
|
| 50 |
|
| 51 | private static <Submit extends SubmitNode> void sortFeatureInto(
|
| 52 | final FeatureRenderPhase.Output output, final SimpleFeatureRenderPhase.FeatureSubmits<Submit> submits
|
| 53 | ) {
|
| 54 | output.acceptFeatureGroup(submits.featureType, maybeShuffle(submits.unbatched), false);
|
| 55 |
|
| 56 | for (List<Submit> batch : maybeShuffle(submits.batches.values())) {
|
| 57 | output.acceptFeatureGroup(submits.featureType, maybeShuffle(batch), false);
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | private static <V> Collection<V> maybeShuffle(final Collection<V> collection) {
|
| 62 | if (SharedConstants.DEBUG_SHUFFLE_MODELS) {
|
| 63 | List<V> shuffled = new ArrayList<>(collection);
|
| 64 | Collections.shuffle(shuffled);
|
| 65 | return shuffled;
|
| 66 | } else {
|
| 67 | return collection;
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | private static <V> V[] maybeShuffle(final V[] array) {
|
| 72 | if (SharedConstants.DEBUG_SHUFFLE_MODELS) {
|
| 73 | V[] shuffled = Arrays.copyOf(array, array.length);
|
| 74 | ArrayUtils.shuffle(shuffled);
|
| 75 | return shuffled;
|
| 76 | } else {
|
| 77 | return array;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | public void clear() {
|
| 82 | for (int i = 0; i < this.submitsByFeature.length; i++) {
|
| 83 | SimpleFeatureRenderPhase.FeatureSubmits<?> submits = this.submitsByFeature[i];
|
| 84 | if (submits != null) {
|
| 85 | if (submits.isEmpty()) {
|
| 86 | this.submitsByFeature[i] = null;
|
| 87 | } else {
|
| 88 | submits.clear();
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public boolean isEmpty() {
|
| 96 | for (SimpleFeatureRenderPhase.FeatureSubmits<?> submits : this.submitsByFeature) {
|
| 97 | if (submits != null && !submits.isEmpty()) {
|
| 98 | return false;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return true;
|
| 103 | }
|
| 104 |
|
| 105 | @Environment(EnvType.CLIENT)
|
| 106 | private static class FeatureSubmits<Submit extends SubmitNode> {
|
| 107 | private final FeatureRendererType<Submit> featureType;
|
| 108 | private final List<Submit> unbatched = new ArrayList<>();
|
| 109 | private final Map<Object, List<Submit>> batches = new HashMap<>();
|
| 110 |
|
| 111 | private FeatureSubmits(final FeatureRendererType<Submit> featureType) {
|
| 112 | this.featureType = featureType;
|
| 113 | }
|
| 114 |
|
| 115 | public void addUnchecked(final SubmitNode submit) {
|
| 116 | this.add((Submit)submit);
|
| 117 | }
|
| 118 |
|
| 119 | public void add(final Submit submit) {
|
| 120 | Object key = batchKey(submit);
|
| 121 | if (key == null) {
|
| 122 | this.unbatched.add(submit);
|
| 123 | } else {
|
| 124 | this.batches.computeIfAbsent(key, var0 -> new ArrayList<>()).add(submit);
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | private static @Nullable Object batchKey(final SubmitNode submit) {
|
| 129 | return submit instanceof BatchableSubmit batchable ? batchable.batchKey() : null;
|
| 130 | }
|
| 131 |
|
| 132 | public boolean isEmpty() {
|
| 133 | if (!this.unbatched.isEmpty()) {
|
| 134 | return false;
|
| 135 | }
|
| 136 |
|
| 137 | for (List<Submit> submits : this.batches.values()) {
|
| 138 | if (!submits.isEmpty()) {
|
| 139 | return false;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | return true;
|
| 144 | }
|
| 145 |
|
| 146 | public void clear() {
|
| 147 | this.unbatched.clear();
|
| 148 | this.batches.values().removeIf(submits -> {
|
| 149 | if (submits.isEmpty()) {
|
| 150 | return true;
|
| 151 | }
|
| 152 |
|
| 153 | submits.clear();
|
| 154 | return false;
|
| 155 | });
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|