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 connection: PlayConnection,
val rendering: Rendering, val rendering: Rendering,
) { ) {
private lateinit var renderThread: Thread
val renderStats = RenderStats() val renderStats = RenderStats()
var screenDimensions = Vec2i(900, 500) var screenDimensions = Vec2i(900, 500)
private set private set
@ -116,6 +117,7 @@ class RenderWindow(
} }
fun init(latch: CountUpAndDownLatch) { fun init(latch: CountUpAndDownLatch) {
renderThread = Thread.currentThread()
Log.log(LogMessageType.RENDERING_LOADING) { "Creating window..." } Log.log(LogMessageType.RENDERING_LOADING) { "Creating window..." }
val stopwatch = Stopwatch() val stopwatch = Stopwatch()
// Setup an error callback. The default implementation // Setup an error callback. The default implementation
@ -406,4 +408,8 @@ class RenderWindow(
fun getClipboardText(): String { fun getClipboardText(): String {
return glfwGetClipboardString(windowId) ?: "" 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() { fun clearChunkCache() {
// ToDo: Stop all preparations // ToDo: Stop all preparations
queuedChunks.clear() queuedChunks.clear()
val chunkMeshes = allChunkSections.toSynchronizedMap().values
allChunkSections.clear()
visibleChunks.clear()
renderWindow.renderQueue.add { renderWindow.renderQueue.add {
visibleChunks.clear() for (meshCollection in chunkMeshes) {
for ((location, _) in allChunkSections) { unloadMeshes(meshCollection.values)
unloadChunk(location)
} }
} }
} }
fun unloadChunk(chunkPosition: Vec2i) { fun unloadChunk(chunkPosition: Vec2i) {
queuedChunks.remove(chunkPosition) queuedChunks.remove(chunkPosition)
renderWindow.renderQueue.add { val chunkMesh = allChunkSections[chunkPosition] ?: return
allChunkSections[chunkPosition]?.let { allChunkSections.remove(chunkPosition)
for ((_, meshCollection) in it) { visibleChunks.remove(chunkPosition)
meshCollection.opaqueSectionArrayMesh.let { renderWindow.renderQueue.add { unloadMeshes(chunkMesh.values) }
it.unload() }
meshes--
triangles -= it.trianglesCount private fun unloadMeshes(meshes: Collection<ChunkMeshCollection>) {
} renderWindow.assertOnRenderThread()
meshCollection.transparentSectionArrayMesh?.let { for (meshCollection in meshes) {
it.unload() meshCollection.opaqueSectionArrayMesh.let {
meshes-- it.unload()
triangles -= it.trianglesCount this.meshes--
} triangles -= it.trianglesCount
} }
allChunkSections.remove(chunkPosition) meshCollection.transparentSectionArrayMesh?.let {
visibleChunks.remove(chunkPosition) it.unload()
this.meshes--
triangles -= it.trianglesCount
} }
} }
} }