Mesh: unload data if not loaded yet

That should fix some memory leaks (especially in entity rendering)
This commit is contained in:
Moritz Zwerger 2023-11-03 15:44:07 +01:00
parent 2d18be7ee4
commit 00ab71e73b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 12 additions and 7 deletions

View File

@ -39,10 +39,14 @@ class ChunkMesher(
}
val sectionNeighbours = ChunkUtil.getDirectNeighbours(neighbours, item.chunk, item.section.sectionHeight)
val mesh = ChunkMeshes(renderer.context, item.chunkPosition, item.sectionHeight, item.section.smallMesh)
solid.mesh(item.chunkPosition, item.sectionHeight, item.chunk, item.section, neighbours, sectionNeighbours, mesh)
try {
solid.mesh(item.chunkPosition, item.sectionHeight, item.chunk, item.section, neighbours, sectionNeighbours, mesh)
if (item.section.blocks.fluidCount > 0) {
fluid.mesh(item.chunkPosition, item.sectionHeight, item.chunk, item.section, neighbours, sectionNeighbours, mesh)
if (item.section.blocks.fluidCount > 0) {
fluid.mesh(item.chunkPosition, item.sectionHeight, item.chunk, item.section, neighbours, sectionNeighbours, mesh)
}
} finally {
mesh.unload()
}
return mesh

View File

@ -135,7 +135,6 @@ class HitboxFeature(renderer: EntityRenderer<*>) : EntityRenderFeature(renderer)
override fun unload() {
val mesh = this.mesh ?: return
this.mesh = null
if (mesh.state != Mesh.MeshStates.LOADED) return
renderer.renderer.queue += { mesh.unload() }
}
}

View File

@ -76,7 +76,6 @@ open class BillboardTextFeature(
val mesh = this.mesh ?: return
this.mesh = null
this.info = null
if (mesh.state != Mesh.MeshStates.LOADED) return
renderer.renderer.queue += { mesh.unload() }
}

View File

@ -83,8 +83,11 @@ abstract class Mesh(
}
fun unload() {
check(state == MeshStates.LOADED) { "Can not unload $state mesh!" }
buffer.unload()
when (state) {
MeshStates.LOADED -> buffer.unload()
MeshStates.PREPARING, MeshStates.FINISHED -> _data?.unload()
else -> throw IllegalStateException("Mesh is already unloaded")
}
state = MeshStates.UNLOADED
}