Compare commits

..
4 changed files with 131 additions and 16 deletions

No files matched your search

@@ -46,10 +46,13 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
private static final Logger LOGGER = LogManager.getLogger("ChunkBuilder");
private final Deque<WrappedTask<T>> buildQueue = new ConcurrentLinkedDeque<>();
// special queue for tasks that will block until the main thread finalizes them
private final Deque<WrappedTask<T>> buildQueueKnownBlocking = new ConcurrentLinkedDeque<>();
private final Deque<ChunkBuildResult<T>> uploadQueue = new ConcurrentLinkedDeque<>();
private final Deque<Throwable> failureQueue = new ConcurrentLinkedDeque<>();
private final Object jobNotifier = new Object();
private final Object jobNotifierKnownBlocking = new Object();
private final AtomicBoolean running = new AtomicBoolean(false);
private final List<Thread> threads = new ArrayList<>();
@@ -60,6 +63,7 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
private Vector3d cameraPosition = new Vector3d();
private final int limitThreads;
private final int limitThreadsBlocking;
private final ChunkVertexType vertexType;
private final ChunkRenderBackend<T> backend;
@@ -67,6 +71,7 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
this.vertexType = vertexType;
this.backend = backend;
this.limitThreads = getThreadCount();
this.limitThreadsBlocking = this.limitThreads;
}
/**
@@ -74,7 +79,7 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
* spawn more tasks than the budget allows, it will block until resources become available.
*/
public int getSchedulingBudget() {
return Math.max(0, (this.limitThreads * TASK_QUEUE_LIMIT_PER_WORKER) - this.buildQueue.size());
return Math.max(0, (this.limitThreads * TASK_QUEUE_LIMIT_PER_WORKER) - this.buildQueue.size() - this.buildQueueKnownBlocking.size());
}
/**
@@ -96,7 +101,7 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
ChunkBuildBuffers buffers = new ChunkBuildBuffers(this.vertexType);
ChunkRenderCacheLocal pipeline = new ChunkRenderCacheLocal(client, this.world);
WorkerRunnable worker = new WorkerRunnable(buffers, pipeline);
WorkerRunnable worker = new WorkerRunnable(i, buffers, pipeline, this.world, false);
Thread thread = new Thread(worker, "Chunk Render Task Executor #" + i);
thread.setPriority(Math.max(0, Thread.NORM_PRIORITY - 2));
@@ -105,6 +110,19 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
this.threads.add(thread);
}
for (int i = 0; i < this.limitThreadsBlocking; i++) {
ChunkBuildBuffers buffers = new ChunkBuildBuffers(this.vertexType);
ChunkRenderCacheLocal pipeline = new ChunkRenderCacheLocal(client, this.world);
WorkerRunnable worker = new WorkerRunnable(this.limitThreads + i, buffers, pipeline, this.world, true);
Thread thread = new Thread(worker, "Chunk Render Blocking Task Executor #" + i);
thread.setPriority(Math.max(0, Thread.NORM_PRIORITY - 2));
thread.start();
this.threads.add(thread);
}
LOGGER.info("Started {} worker threads", this.threads.size());
}
@@ -137,6 +155,9 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
synchronized (this.jobNotifier) {
this.jobNotifier.notifyAll();
}
synchronized (this.jobNotifierKnownBlocking) {
this.jobNotifierKnownBlocking.notifyAll();
}
// Keep processing the main thread tasks so the workers don't block forever
AngelicaRenderQueue.managedBlock(() -> !workersAlive());
@@ -159,7 +180,12 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
job.future.cancel(true);
}
for (WrappedTask<?> job : this.buildQueueKnownBlocking) {
job.future.cancel(true);
}
this.buildQueue.clear();
this.buildQueueKnownBlocking.clear();
this.world = null;
this.sectionCache = null;
@@ -200,7 +226,7 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
if (this.uploadQueue.isEmpty()) {
return false;
}
this.backend.upload(RenderDevice.INSTANCE.createCommandList(), filterChunkBuilds(new DequeDrain<>(this.uploadQueue)));
return true;
@@ -256,7 +282,7 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
* @return True if the build queue is empty
*/
public boolean isBuildQueueEmpty() {
return this.buildQueue.isEmpty();
return this.buildQueue.isEmpty() && this.buildQueueKnownBlocking.isEmpty();
}
/**
@@ -388,9 +414,18 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
// caches between different CPU cores
private final ChunkRenderCacheLocal cache;
public WorkerRunnable(ChunkBuildBuffers bufferCache, ChunkRenderCacheLocal cache) {
private final WorldClient world;
private final int i;
private final boolean allowBlocking;
public WorkerRunnable(int i, ChunkBuildBuffers bufferCache, ChunkRenderCacheLocal cache, WorldClient world, boolean allowBlocking) {
this.i = i;
this.bufferCache = bufferCache;
this.cache = cache;
this.world = world;
this.allowBlocking = allowBlocking;
}
@Override
@@ -404,6 +439,15 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
continue;
}
// Relegate tasks that can block for an extended period of time to a separate threadpool.
if (!this.allowBlocking && job.task.willRenderInMainThread(this.cache)) {
ChunkBuilder.this.buildQueueKnownBlocking.add(job);
synchronized (ChunkBuilder.this.jobNotifierKnownBlocking) {
ChunkBuilder.this.jobNotifierKnownBlocking.notify();
}
continue;
}
ChunkBuildResult<T> result;
try {
@@ -435,18 +479,33 @@ public class ChunkBuilder<T extends ChunkGraphicsState> {
* currently available, it will wait on {@link ChunkBuilder#jobNotifier} field until notified.
*/
private WrappedTask<T> getNextJob() {
WrappedTask<T> job = ChunkBuilder.this.buildQueue.poll();
if (this.allowBlocking) {
WrappedTask<T> job = ChunkBuilder.this.buildQueueKnownBlocking.poll();
if (job == null) {
synchronized (ChunkBuilder.this.jobNotifier) {
try {
ChunkBuilder.this.jobNotifier.wait();
} catch (InterruptedException ignored) {
if (job == null) {
synchronized (ChunkBuilder.this.jobNotifierKnownBlocking) {
try {
ChunkBuilder.this.jobNotifierKnownBlocking.wait();
} catch (InterruptedException ignored) {
}
}
}
}
return job;
return job;
} else {
WrappedTask<T> job = ChunkBuilder.this.buildQueue.poll();
if (job == null) {
synchronized (ChunkBuilder.this.jobNotifier) {
try {
ChunkBuilder.this.jobNotifier.wait();
} catch (InterruptedException ignored) {
}
}
}
return job;
}
}
}
@@ -32,6 +32,7 @@ public abstract class ChunkRenderBuildTask<T extends ChunkGraphicsState> {
public abstract ChunkBuildResult<T> performBuild(ChunkRenderCacheLocal cache, ChunkBuildBuffers buffers,
CancellationSource cancellationSource);
public boolean willRenderInMainThread(ChunkRenderCacheLocal cache) { return false; };
/**
* Called on the main render thread when the task's execution returns. The implementation should release any
* resources it's still holding onto at this point.
@@ -61,13 +61,15 @@ public class ChunkRenderRebuildTask<T extends ChunkGraphicsState> extends ChunkR
private final boolean translucencySorting;
private boolean hasMainThreadBlocks = false;
private boolean hasMainThreadBlocksComputed = false;
public ChunkRenderRebuildTask(ChunkRenderContainer<T> render, ChunkRenderContext context, BlockPos offset) {
this.render = render;
this.offset = offset;
this.context = context;
this.camera = new Vector3d();
this.translucencySorting = SodiumClientMod.options().advanced.translucencySorting;
}
public ChunkRenderRebuildTask<T> withCameraPosition(Vector3d camera) {
@@ -122,6 +124,48 @@ public class ChunkRenderRebuildTask<T extends ChunkGraphicsState> extends ChunkR
return canRender;
}
public boolean willRenderInMainThread(ChunkRenderCacheLocal cache) {
if (hasMainThreadBlocksComputed)
return hasMainThreadBlocks;
final ChunkRenderData.Builder renderData = new ChunkRenderData.Builder();
final ChunkOcclusionDataBuilder occluder = new ChunkOcclusionDataBuilder();
final ChunkRenderBounds.Builder bounds = new ChunkRenderBounds.Builder();
cache.init(this.context);
final WorldSlice slice = cache.getWorldSlice();
final int baseX = this.render.getOriginX();
final int baseY = this.render.getOriginY();
final int baseZ = this.render.getOriginZ();
final BlockPos pos = new BlockPos();
outer:
for (int relY = 0; relY < 16; relY++) {
for (int relZ = 0; relZ < 16; relZ++) {
for (int relX = 0; relX < 16; relX++) {
final Block block = slice.getBlockRelative(relX + 16, relY + 16, relZ + 16);
// If the block is vanilla air, assume it renders nothing. Don't use isAir because mods
// can abuse it for all sorts of things
if (block.getMaterial() == Material.air) {
continue;
}
if (!rendersOffThread(block)) {
hasMainThreadBlocks = true;
break outer;
}
}
}
}
hasMainThreadBlocksComputed = true;
return hasMainThreadBlocks;
}
@Override
public ChunkBuildResult<T> performBuild(ChunkRenderCacheLocal cache, ChunkBuildBuffers buffers, CancellationSource cancellationSource) {
final ChunkRenderData.Builder renderData = new ChunkRenderData.Builder();
@@ -147,7 +191,6 @@ public class ChunkRenderRebuildTask<T extends ChunkGraphicsState> extends ChunkR
final BlockPos renderOffset = this.offset;
final LongArrayFIFOQueue mainThreadBlocks = new LongArrayFIFOQueue();
boolean hasMainThreadBlocks = false;
for (int relY = 0; relY < 16; relY++) {
if (cancellationSource.isCancelled()) {
@@ -242,6 +285,7 @@ public class ChunkRenderRebuildTask<T extends ChunkGraphicsState> extends ChunkR
handleRenderBlocksTextures(renderBlocks, renderData);
hasMainThreadBlocksComputed = true;
if(hasMainThreadBlocks) {
// Render the other blocks on the main thread
var future = CompletableFuture.runAsync(() -> this.performMainBuild(cache, buffers, cancellationSource, bounds, renderData, mainThreadBlocks), AngelicaRenderQueue.executor());
@@ -350,6 +394,8 @@ public class ChunkRenderRebuildTask<T extends ChunkGraphicsState> extends ChunkR
+ camera
+ ", translucencySorting="
+ translucencySorting
+ ", hasMainThreadBlocks="
+ hasMainThreadBlocks
+ '}';
}
@@ -136,7 +136,16 @@ public class MixinRenderGlobal implements IRenderGlobalExt {
*/
@Overwrite
public boolean updateRenderers(EntityLivingBase e, boolean b){
AngelicaRenderQueue.processTasks(1);
final int BUDGET_NS = 5 * 1000 * 1000;
long startTime = System.nanoTime();
for (;;) {
int tasksRan = AngelicaRenderQueue.processTasks(1);
if (tasksRan == 0)
break;
long elapsedTime = System.nanoTime() - startTime;
if (elapsedTime > BUDGET_NS)
break;
}
return true;
}