From 191c2e665e925668c25feff014533d53ebd1e0f0 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sat, 23 Oct 2021 15:12:59 +0200 Subject: [PATCH] rendering: reduce memory usage and peaks --- .../gui/rendering/gui/elements/Element.kt | 1 + .../minosoft/gui/rendering/util/mesh/Mesh.kt | 1 + .../util/collections/ArrayFloatList.kt | 29 ++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/Element.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/Element.kt index 92183254b..ca109ced3 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/Element.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/Element.kt @@ -125,6 +125,7 @@ abstract class Element(val hudRenderer: HUDRenderer) { cache.z = z val maxZ = forceRender(offset, z, cache, options) cache.maxZ = maxZ + cache.data.finalize() this.cache = cache cacheUpToDate = true } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/util/mesh/Mesh.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/util/mesh/Mesh.kt index be80d20e7..2dbca82a7 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/util/mesh/Mesh.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/util/mesh/Mesh.kt @@ -44,6 +44,7 @@ abstract class Mesh( fun load() { buffer = renderWindow.renderSystem.createVertexBuffer(struct, data.toArray(), primitiveType) + _data = null buffer.init() vertices = buffer.vertices } diff --git a/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt b/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt index 86f64d35f..8dbeadebd 100644 --- a/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt +++ b/src/main/java/de/bixilon/minosoft/util/collections/ArrayFloatList.kt @@ -17,6 +17,8 @@ class ArrayFloatList( private val initialSize: Int = DEFAULT_INITIAL_SIZE, ) { private var data: FloatArray = FloatArray(initialSize) + var finalized: Boolean = false + private set val limit: Int get() = data.size var size = 0 @@ -33,13 +35,21 @@ class ArrayFloatList( private var output: FloatArray = FloatArray(0) private var outputUpToDate = false + private fun checkFinalized() { + if (finalized) { + throw IllegalStateException("ArrayFloatList is already finalized!") + } + } + fun clear() { + checkFinalized() size = 0 outputUpToDate = false output = FloatArray(0) } private fun ensureSize(needed: Int) { + checkFinalized() if (limit - size >= needed) { return } @@ -65,10 +75,15 @@ class ArrayFloatList( outputUpToDate = false } - fun addAll(floats: ArrayFloatList) { - ensureSize(floats.size) - System.arraycopy(floats.data, 0, data, size, floats.size) - size += floats.size + fun addAll(floatList: ArrayFloatList) { + ensureSize(floatList.size) + val source = if (floatList.finalized) { + floatList.output + } else { + floatList.data + } + System.arraycopy(source, 0, data, size, floatList.size) + size += floatList.size } private fun checkOutputArray() { @@ -85,6 +100,12 @@ class ArrayFloatList( return output } + fun finalize() { + finalized = true + checkOutputArray() + data = FloatArray(0) + } + private companion object { private const val DEFAULT_INITIAL_SIZE = 1000