76 lines
1.9 KiB
Java
76 lines
1.9 KiB
Java
package net.minecraft.client.renderer.chunk;
|
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
|
import java.util.List;
|
|
import java.util.ListIterator;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.renderer.chunk.SectionRenderDispatcher.RenderSection.CompileTask;
|
|
import net.minecraft.world.phys.Vec3;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class CompileTaskDynamicQueue {
|
|
private static final int MAX_RECOMPILE_QUOTA = 2;
|
|
private int recompileQuota = 2;
|
|
private final List<CompileTask> tasks = new ObjectArrayList<>();
|
|
|
|
public synchronized void add(CompileTask task) {
|
|
this.tasks.add(task);
|
|
}
|
|
|
|
@Nullable
|
|
public synchronized CompileTask poll(Vec3 cameraPosition) {
|
|
int i = -1;
|
|
int j = -1;
|
|
double d = Double.MAX_VALUE;
|
|
double e = Double.MAX_VALUE;
|
|
ListIterator<CompileTask> listIterator = this.tasks.listIterator();
|
|
|
|
while (listIterator.hasNext()) {
|
|
int k = listIterator.nextIndex();
|
|
CompileTask compileTask = (CompileTask)listIterator.next();
|
|
if (compileTask.isCancelled.get()) {
|
|
listIterator.remove();
|
|
} else {
|
|
double f = compileTask.getRenderOrigin().distToCenterSqr(cameraPosition);
|
|
if (!compileTask.isRecompile() && f < d) {
|
|
d = f;
|
|
i = k;
|
|
}
|
|
|
|
if (compileTask.isRecompile() && f < e) {
|
|
e = f;
|
|
j = k;
|
|
}
|
|
}
|
|
}
|
|
|
|
boolean bl = j >= 0;
|
|
boolean bl2 = i >= 0;
|
|
if (!bl || bl2 && (this.recompileQuota <= 0 || !(e < d))) {
|
|
this.recompileQuota = 2;
|
|
return this.removeTaskByIndex(i);
|
|
} else {
|
|
this.recompileQuota--;
|
|
return this.removeTaskByIndex(j);
|
|
}
|
|
}
|
|
|
|
public int size() {
|
|
return this.tasks.size();
|
|
}
|
|
|
|
@Nullable
|
|
private CompileTask removeTaskByIndex(int index) {
|
|
return index >= 0 ? (CompileTask)this.tasks.remove(index) : null;
|
|
}
|
|
|
|
public synchronized void clear() {
|
|
for (CompileTask compileTask : this.tasks) {
|
|
compileTask.cancel();
|
|
}
|
|
|
|
this.tasks.clear();
|
|
}
|
|
}
|