Better chunk load order, and better loading priority.

This commit is contained in:
IntegratedQuantum 2022-10-04 16:40:42 +02:00
parent aa3a769dbe
commit bb2786c36f
3 changed files with 17 additions and 23 deletions

View File

@ -93,6 +93,10 @@ pub const ChunkPosition = struct {
dz = @maximum(0, dz - halfWidth); dz = @maximum(0, dz - halfWidth);
return dx*dx + dy*dy + dz*dz; return dx*dx + dy*dy + dz*dz;
} }
pub fn getPriority(self: ChunkPosition, playerPos: Vec3d) f32 {
return -@floatCast(f32, self.getMinDistanceSquared(playerPos))/@intToFloat(f32, self.voxelSize*self.voxelSize) + 2*@intToFloat(f32, std.math.log2_int(UChunkCoordinate, self.voxelSize)*chunkSize*chunkSize);
}
}; };
pub const Chunk = struct { pub const Chunk = struct {

View File

@ -341,6 +341,7 @@ pub const ConnectionManager = struct {
errdefer Socket.deinit(result.socket); errdefer Socket.deinit(result.socket);
result.thread = try std.Thread.spawn(.{}, run, .{result}); result.thread = try std.Thread.spawn(.{}, run, .{result});
try result.thread.setName("Network Thread");
if(online) { if(online) {
result.makeOnline(); result.makeOnline();
} }
@ -743,27 +744,6 @@ pub const Protocols = blk: {
// Bits.putInt(data, 8, ch.wz); // Bits.putInt(data, 8, ch.wz);
// Bits.putInt(data, 12, ch.voxelSize); // Bits.putInt(data, 12, ch.voxelSize);
// conn.sendImportant(this, data); // conn.sendImportant(this, data);
// }
//
// private static class ChunkLoadTask extends ThreadPool.Task {
// private final VisibleChunk ch;
// public ChunkLoadTask(VisibleChunk ch) {
// this.ch = ch;
// }
// @Override
// public float getPriority() {
// return ch.getPriority(Cubyz.player);
// }
//
// @Override
// public boolean isStillNeeded() {
// return Cubyz.chunkTree.findNode(ch) != null;
// }
//
// @Override
// public void run() {
// ch.load();
// }
// } // }
}), }),
playerPosition: type = addProtocol(&comptimeList, struct { playerPosition: type = addProtocol(&comptimeList, struct {

View File

@ -658,7 +658,17 @@ pub const RenderStructure = struct {
mutex.lock(); mutex.lock();
defer mutex.unlock(); defer mutex.unlock();
while(updatableList.items.len != 0) { while(updatableList.items.len != 0) {
const pos = updatableList.orderedRemove(0); // TODO: Find a faster solution than going through the entire list.
var closestPriority: f32 = -std.math.floatMax(f32);
var closestIndex: usize = 0;
for(updatableList.items) |pos, i| {
const priority = pos.getPriority(game.playerPos);
if(priority > closestPriority) {
closestPriority = priority;
closestIndex = i;
}
}
const pos = updatableList.orderedRemove(closestIndex);
mutex.unlock(); mutex.unlock();
defer mutex.lock(); defer mutex.lock();
const nullNode = _getNode(pos); const nullNode = _getNode(pos);
@ -698,7 +708,7 @@ pub const RenderStructure = struct {
} }
pub fn getPriority(self: *MeshGenerationTask) f32 { pub fn getPriority(self: *MeshGenerationTask) f32 {
return -@floatCast(f32, self.mesh.pos.getMinDistanceSquared(game.playerPos))/@intToFloat(f32, self.mesh.pos.voxelSize*self.mesh.pos.voxelSize); return self.mesh.pos.getPriority(game.playerPos);
} }
pub fn isStillNeeded(self: *MeshGenerationTask) bool { pub fn isStillNeeded(self: *MeshGenerationTask) bool {