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 cache.z = z
val maxZ = forceRender(offset, z, cache, options) val maxZ = forceRender(offset, z, cache, options)
cache.maxZ = maxZ cache.maxZ = maxZ
cache.data.finalize()
this.cache = cache this.cache = cache
cacheUpToDate = true cacheUpToDate = true
} }

View File

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

View File

@ -17,6 +17,8 @@ class ArrayFloatList(
private val initialSize: Int = DEFAULT_INITIAL_SIZE, private val initialSize: Int = DEFAULT_INITIAL_SIZE,
) { ) {
private var data: FloatArray = FloatArray(initialSize) private var data: FloatArray = FloatArray(initialSize)
var finalized: Boolean = false
private set
val limit: Int val limit: Int
get() = data.size get() = data.size
var size = 0 var size = 0
@ -33,13 +35,21 @@ class ArrayFloatList(
private var output: FloatArray = FloatArray(0) private var output: FloatArray = FloatArray(0)
private var outputUpToDate = false private var outputUpToDate = false
private fun checkFinalized() {
if (finalized) {
throw IllegalStateException("ArrayFloatList is already finalized!")
}
}
fun clear() { fun clear() {
checkFinalized()
size = 0 size = 0
outputUpToDate = false outputUpToDate = false
output = FloatArray(0) output = FloatArray(0)
} }
private fun ensureSize(needed: Int) { private fun ensureSize(needed: Int) {
checkFinalized()
if (limit - size >= needed) { if (limit - size >= needed) {
return return
} }
@ -65,10 +75,15 @@ class ArrayFloatList(
outputUpToDate = false outputUpToDate = false
} }
fun addAll(floats: ArrayFloatList) { fun addAll(floatList: ArrayFloatList) {
ensureSize(floats.size) ensureSize(floatList.size)
System.arraycopy(floats.data, 0, data, size, floats.size) val source = if (floatList.finalized) {
size += floats.size floatList.output
} else {
floatList.data
}
System.arraycopy(source, 0, data, size, floatList.size)
size += floatList.size
} }
private fun checkOutputArray() { private fun checkOutputArray() {
@ -85,6 +100,12 @@ class ArrayFloatList(
return output return output
} }
fun finalize() {
finalized = true
checkOutputArray()
data = FloatArray(0)
}
private companion object { private companion object {
private const val DEFAULT_INITIAL_SIZE = 1000 private const val DEFAULT_INITIAL_SIZE = 1000