reuse FloatBuffer in GUIMesh (reduce memory allocations)

This commit is contained in:
Bixilon 2021-11-17 19:48:08 +01:00
parent 66f4f74848
commit e2f4170bb8
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 19 additions and 8 deletions

View File

@ -48,4 +48,3 @@
- Reduce memory usage
- Unload on minimize
- Biomes: Check if chunk is single biome
- Texture rotation in hopper

View File

@ -49,6 +49,7 @@ import de.bixilon.minosoft.util.KUtil.synchronizedMapOf
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.KUtil.toSynchronizedMap
import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.collections.DirectArrayFloatList
import glm_.glm
import glm_.mat4x4.Mat4
import glm_.vec2.Vec2
@ -153,11 +154,15 @@ class HUDRenderer(
}
override fun drawOther() {
if (this::mesh.isInitialized) {
val data = if (this::mesh.isInitialized) {
mesh.unload()
mesh.data.buffer.clear()
mesh.data
} else {
DirectArrayFloatList()
}
mesh = GUIMesh(renderWindow, matrix)
mesh = GUIMesh(renderWindow, matrix, data)
val hudElements = hudElements.toSynchronizedMap().values
val time = System.currentTimeMillis()

View File

@ -25,6 +25,7 @@ import de.bixilon.minosoft.gui.rendering.input.camera.hit.BlockRaycastHit
import de.bixilon.minosoft.gui.rendering.input.camera.hit.EntityRaycastHit
import de.bixilon.minosoft.gui.rendering.system.base.BlendingFunctions
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.collections.DirectArrayFloatList
class CrosshairHUDElement(hudRenderer: HUDRenderer) : CustomHUDElement(hudRenderer) {
private lateinit var crosshairAtlasElement: HUDAtlasElement
@ -62,7 +63,7 @@ class CrosshairHUDElement(hudRenderer: HUDRenderer) : CustomHUDElement(hudRender
val config = Minosoft.config.config.game.hud.crosshair
val mesh = GUIMesh(renderWindow, hudRenderer.matrix)
val mesh = GUIMesh(renderWindow, hudRenderer.matrix, DirectArrayFloatList(42))
// Custom draw to make the crosshair inverted
if (renderWindow.connection.player.gamemode == Gamemodes.SPECTATOR) {

View File

@ -19,6 +19,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import de.bixilon.minosoft.util.collections.DirectArrayFloatList
import glm_.mat4x4.Mat4
import glm_.vec2.Vec2
import glm_.vec2.Vec2t
@ -28,7 +29,8 @@ import glm_.vec4.Vec4
class GUIMesh(
renderWindow: RenderWindow,
val matrix: Mat4,
) : Mesh(renderWindow, GUIMeshStruct, initialCacheSize = 40000), GUIVertexConsumer {
data: DirectArrayFloatList,
) : Mesh(renderWindow, GUIMeshStruct, initialCacheSize = 40000, clearOnLoad = false, data = data), GUIVertexConsumer {
override fun addVertex(position: Vec2t<*>, z: Int, texture: AbstractTexture, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
data.addAll(createVertex(matrix, position, z, texture, uv, tint, options))

View File

@ -25,9 +25,11 @@ abstract class Mesh(
private val struct: MeshStruct,
private val primitiveType: PrimitiveTypes = renderWindow.renderSystem.preferredPrimitiveType,
initialCacheSize: Int = 10000,
val clearOnLoad: Boolean = true,
data: DirectArrayFloatList? = null,
) {
val order = renderWindow.renderSystem.primitiveMeshOrder
private var _data: DirectArrayFloatList? = DirectArrayFloatList(initialCacheSize)
private var _data: DirectArrayFloatList? = data ?: DirectArrayFloatList(initialCacheSize)
var data: DirectArrayFloatList
get() = _data!!
set(value) {
@ -46,8 +48,10 @@ abstract class Mesh(
fun load() {
buffer = renderWindow.renderSystem.createVertexBuffer(struct, data.buffer, primitiveType)
buffer.init()
data.unload()
_data = null
if (clearOnLoad) {
data.unload()
_data = null
}
vertices = buffer.vertices
}