rendering: WorldRenderer: improve chunk cache clearing

This commit is contained in:
Bixilon 2021-05-03 14:08:33 +02:00
parent 3c6baf0935
commit 6bb865867e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 29 additions and 19 deletions

View File

@ -56,6 +56,7 @@ class RenderWindow(
val connection: PlayConnection,
val rendering: Rendering,
) {
private lateinit var renderThread: Thread
val renderStats = RenderStats()
var screenDimensions = Vec2i(900, 500)
private set
@ -116,6 +117,7 @@ class RenderWindow(
}
fun init(latch: CountUpAndDownLatch) {
renderThread = Thread.currentThread()
Log.log(LogMessageType.RENDERING_LOADING) { "Creating window..." }
val stopwatch = Stopwatch()
// Setup an error callback. The default implementation
@ -406,4 +408,8 @@ class RenderWindow(
fun getClipboardText(): String {
return glfwGetClipboardString(windowId) ?: ""
}
fun assertOnRenderThread() {
check(Thread.currentThread() == renderThread) { "Current thread (${Thread.currentThread().name} is not the render thread!" }
}
}

View File

@ -351,32 +351,36 @@ class WorldRenderer(
fun clearChunkCache() {
// ToDo: Stop all preparations
queuedChunks.clear()
val chunkMeshes = allChunkSections.toSynchronizedMap().values
allChunkSections.clear()
visibleChunks.clear()
renderWindow.renderQueue.add {
visibleChunks.clear()
for ((location, _) in allChunkSections) {
unloadChunk(location)
for (meshCollection in chunkMeshes) {
unloadMeshes(meshCollection.values)
}
}
}
fun unloadChunk(chunkPosition: Vec2i) {
queuedChunks.remove(chunkPosition)
renderWindow.renderQueue.add {
allChunkSections[chunkPosition]?.let {
for ((_, meshCollection) in it) {
meshCollection.opaqueSectionArrayMesh.let {
it.unload()
meshes--
triangles -= it.trianglesCount
}
meshCollection.transparentSectionArrayMesh?.let {
it.unload()
meshes--
triangles -= it.trianglesCount
}
}
allChunkSections.remove(chunkPosition)
visibleChunks.remove(chunkPosition)
val chunkMesh = allChunkSections[chunkPosition] ?: return
allChunkSections.remove(chunkPosition)
visibleChunks.remove(chunkPosition)
renderWindow.renderQueue.add { unloadMeshes(chunkMesh.values) }
}
private fun unloadMeshes(meshes: Collection<ChunkMeshCollection>) {
renderWindow.assertOnRenderThread()
for (meshCollection in meshes) {
meshCollection.opaqueSectionArrayMesh.let {
it.unload()
this.meshes--
triangles -= it.trianglesCount
}
meshCollection.transparentSectionArrayMesh?.let {
it.unload()
this.meshes--
triangles -= it.trianglesCount
}
}
}