| 1 | package net.minecraft.client.renderer.chunk;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.ListIterator;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.renderer.chunk.SectionRenderDispatcher.RenderSection.SectionTask;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class SectionTaskDynamicQueue {
|
| 14 | private static final int MAX_RECOMPILE_QUOTA = 2;
|
| 15 | private int recompileQuota = 2;
|
| 16 | private final List<SectionTask> tasks = new ObjectArrayList<>();
|
| 17 |
|
| 18 | public synchronized void add(final SectionTask task) {
|
| 19 | this.tasks.add(task);
|
| 20 | }
|
| 21 |
|
| 22 | public synchronized SectionTask/* @Nullable */ poll(final Vec3 cameraPos) {
|
| 23 | int bestInitialCompileTaskIndex = -1;
|
| 24 | int bestRecompileTaskIndex = -1;
|
| 25 | double bestInitialCompileDistance = Double.MAX_VALUE;
|
| 26 | double bestRecompileDistance = Double.MAX_VALUE;
|
| 27 | ListIterator<SectionTask> iterator = this.tasks.listIterator();
|
| 28 |
|
| 29 | while (iterator.hasNext()) {
|
| 30 | int taskIndex = iterator.nextIndex();
|
| 31 | SectionTask task = iterator.next();
|
| 32 | if (task.isCancelled.get()) {
|
| 33 | iterator.remove();
|
| 34 | } else {
|
| 35 | double distance = task.getRenderOrigin().distToCenterSqr(cameraPos);
|
| 36 | if (!task.isRecompile() && distance < bestInitialCompileDistance) {
|
| 37 | bestInitialCompileDistance = distance;
|
| 38 | bestInitialCompileTaskIndex = taskIndex;
|
| 39 | }
|
| 40 |
|
| 41 | if (task.isRecompile() && distance < bestRecompileDistance) {
|
| 42 | bestRecompileDistance = distance;
|
| 43 | bestRecompileTaskIndex = taskIndex;
|
| 44 | }
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | boolean hasRecompileTask = bestRecompileTaskIndex >= 0;
|
| 49 | boolean hasInitialCompileTask = bestInitialCompileTaskIndex >= 0;
|
| 50 | if (!hasRecompileTask || hasInitialCompileTask && (this.recompileQuota <= 0 || !(bestRecompileDistance < bestInitialCompileDistance))) {
|
| 51 | this.recompileQuota = 2;
|
| 52 | return this.removeTaskByIndex(bestInitialCompileTaskIndex);
|
| 53 | } else {
|
| 54 | this.recompileQuota--;
|
| 55 | return this.removeTaskByIndex(bestRecompileTaskIndex);
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | public int size() {
|
| 60 | return this.tasks.size();
|
| 61 | }
|
| 62 |
|
| 63 | private SectionTask/* @Nullable */ removeTaskByIndex(final int taskIndex) {
|
| 64 | return taskIndex >= 0 ? this.tasks.remove(taskIndex) : null;
|
| 65 | }
|
| 66 |
|
| 67 | public synchronized void clear() {
|
| 68 | for (SectionTask task : this.tasks) {
|
| 69 | task.cancel();
|
| 70 | }
|
| 71 |
|
| 72 | this.tasks.clear();
|
| 73 | }
|
| 74 | }
|
| 75 |
|