From 6bb865867e2c0846d1656346e388ee3383c858be Mon Sep 17 00:00:00 2001 From: Bixilon Date: Mon, 3 May 2021 14:08:33 +0200 Subject: [PATCH] rendering: WorldRenderer: improve chunk cache clearing --- .../minosoft/gui/rendering/RenderWindow.kt | 6 +++ .../gui/rendering/chunk/WorldRenderer.kt | 42 ++++++++++--------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 1bdd6b59e..f6b40f5c3 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -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!" } + } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt index e5acae7b7..05d686538 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt @@ -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) { + 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 } } }