rendering: reduce memory usage and peaks

This commit is contained in:
Bixilon 2021-10-23 15:12:59 +02:00
parent 99fb1c35ef
commit 191c2e665e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 27 additions and 4 deletions

View File

@ -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
}

View File

@ -44,6 +44,7 @@ abstract class Mesh(
fun load() {
buffer = renderWindow.renderSystem.createVertexBuffer(struct, data.toArray(), primitiveType)
_data = null
buffer.init()
vertices = buffer.vertices
}

View File

@ -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