chunk queue comparator

This reduces meshing overhead and sorting time minimal
This commit is contained in:
Bixilon 2023-06-01 00:16:45 +02:00
parent 42a94ab3d1
commit c70a88d78d
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 47 additions and 12 deletions

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -13,7 +13,6 @@
package de.bixilon.minosoft.gui.rendering.world.queue.meshing
import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.concurrent.lock.simple.SimpleLock
import de.bixilon.kutil.concurrent.pool.ThreadPool
@ -29,6 +28,7 @@ import de.bixilon.minosoft.util.SystemInformation
class ChunkMeshingQueue(
private val renderer: WorldRenderer,
) {
private val comparator = ChunkQueueComparator()
val tasks = MeshPrepareTaskManager(renderer)
val maxMeshesToLoad = if (SystemInformation.RUNTIME.maxMemory() > 1_000_000_000) 150 else 80
@ -45,16 +45,8 @@ class ChunkMeshingQueue(
fun sort() {
lock()
val position = renderer.cameraChunkPosition
val height = renderer.cameraSectionHeight
val cameraSectionPosition = Vec3i(position.x, height, position.y)
queue.sortBy {
if (it.chunkPosition == position) {
// our own chunk always has the highest priority
return@sortBy -Int.MAX_VALUE
}
(it.sectionPosition - cameraSectionPosition).length2()
}
comparator.update(renderer)
queue.sortWith(comparator)
unlock()
}

View File

@ -0,0 +1,43 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.world.queue.meshing
import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.minosoft.data.world.positions.ChunkPosition
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY
import de.bixilon.minosoft.gui.rendering.world.WorldQueueItem
import de.bixilon.minosoft.gui.rendering.world.WorldRenderer
class ChunkQueueComparator : Comparator<WorldQueueItem> {
private var position: ChunkPosition = Vec2i.EMPTY
private var height = 0
fun update(renderer: WorldRenderer) {
this.position = renderer.cameraChunkPosition
this.height = renderer.cameraSectionHeight
}
private fun compare(item: WorldQueueItem): Int {
val array = item.sectionPosition.array
val x = array[0] - position.x
val y = array[1] - height
val z = array[2] - position.y
return (x * x + y * y + z * z)
}
override fun compare(a: WorldQueueItem, b: WorldQueueItem): Int {
return compare(a).compareTo(compare(b))
}
}