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 - Reduce memory usage
- Unload on minimize - Unload on minimize
- Biomes: Check if chunk is single biome - 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.toResourceLocation
import de.bixilon.minosoft.util.KUtil.toSynchronizedMap import de.bixilon.minosoft.util.KUtil.toSynchronizedMap
import de.bixilon.minosoft.util.KUtil.unsafeCast import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.collections.DirectArrayFloatList
import glm_.glm import glm_.glm
import glm_.mat4x4.Mat4 import glm_.mat4x4.Mat4
import glm_.vec2.Vec2 import glm_.vec2.Vec2
@ -153,11 +154,15 @@ class HUDRenderer(
} }
override fun drawOther() { override fun drawOther() {
if (this::mesh.isInitialized) { val data = if (this::mesh.isInitialized) {
mesh.unload() 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 hudElements = hudElements.toSynchronizedMap().values
val time = System.currentTimeMillis() 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.input.camera.hit.EntityRaycastHit
import de.bixilon.minosoft.gui.rendering.system.base.BlendingFunctions import de.bixilon.minosoft.gui.rendering.system.base.BlendingFunctions
import de.bixilon.minosoft.util.KUtil.toResourceLocation import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.collections.DirectArrayFloatList
class CrosshairHUDElement(hudRenderer: HUDRenderer) : CustomHUDElement(hudRenderer) { class CrosshairHUDElement(hudRenderer: HUDRenderer) : CustomHUDElement(hudRenderer) {
private lateinit var crosshairAtlasElement: HUDAtlasElement private lateinit var crosshairAtlasElement: HUDAtlasElement
@ -62,7 +63,7 @@ class CrosshairHUDElement(hudRenderer: HUDRenderer) : CustomHUDElement(hudRender
val config = Minosoft.config.config.game.hud.crosshair 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 // Custom draw to make the crosshair inverted
if (renderWindow.connection.player.gamemode == Gamemodes.SPECTATOR) { 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.system.base.texture.texture.AbstractTexture
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import de.bixilon.minosoft.util.collections.DirectArrayFloatList
import glm_.mat4x4.Mat4 import glm_.mat4x4.Mat4
import glm_.vec2.Vec2 import glm_.vec2.Vec2
import glm_.vec2.Vec2t import glm_.vec2.Vec2t
@ -28,7 +29,8 @@ import glm_.vec4.Vec4
class GUIMesh( class GUIMesh(
renderWindow: RenderWindow, renderWindow: RenderWindow,
val matrix: Mat4, 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?) { 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)) 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 struct: MeshStruct,
private val primitiveType: PrimitiveTypes = renderWindow.renderSystem.preferredPrimitiveType, private val primitiveType: PrimitiveTypes = renderWindow.renderSystem.preferredPrimitiveType,
initialCacheSize: Int = 10000, initialCacheSize: Int = 10000,
val clearOnLoad: Boolean = true,
data: DirectArrayFloatList? = null,
) { ) {
val order = renderWindow.renderSystem.primitiveMeshOrder val order = renderWindow.renderSystem.primitiveMeshOrder
private var _data: DirectArrayFloatList? = DirectArrayFloatList(initialCacheSize) private var _data: DirectArrayFloatList? = data ?: DirectArrayFloatList(initialCacheSize)
var data: DirectArrayFloatList var data: DirectArrayFloatList
get() = _data!! get() = _data!!
set(value) { set(value) {
@ -46,8 +48,10 @@ abstract class Mesh(
fun load() { fun load() {
buffer = renderWindow.renderSystem.createVertexBuffer(struct, data.buffer, primitiveType) buffer = renderWindow.renderSystem.createVertexBuffer(struct, data.buffer, primitiveType)
buffer.init() buffer.init()
if (clearOnLoad) {
data.unload() data.unload()
_data = null _data = null
}
vertices = buffer.vertices vertices = buffer.vertices
} }