improve palette performance, improve hud performance

This commit is contained in:
Bixilon 2022-05-11 20:26:23 +02:00
parent 0f0441d409
commit 7fcda64a8d
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 19 additions and 16 deletions

View File

@ -21,6 +21,8 @@ class ArrayPaletteData(
val elementBits: Int, val elementBits: Int,
override val size: Int, override val size: Int,
) : PaletteData { ) : PaletteData {
private val singleValueMask = (1 shl elementBits) - 1
private val valuesPerLong = Long.SIZE_BITS / elementBits
private lateinit var data: LongArray private lateinit var data: LongArray
init { init {
@ -34,17 +36,14 @@ class ArrayPaletteData(
(bits + (Long.SIZE_BITS - 1)) / Long.SIZE_BITS // divide up (bits + (Long.SIZE_BITS - 1)) / Long.SIZE_BITS // divide up
} else { } else {
val elementsPerLong = Long.SIZE_BITS / elementBits (size + valuesPerLong - 1) / valuesPerLong
(size + elementsPerLong - 1) / elementsPerLong
} }
data = buffer.readLongArray(longs) data = buffer.readLongArray(longs)
} }
override operator fun get(index: Int): Int { override operator fun get(index: Int): Int {
val individualValueMask = (1 shl elementBits) - 1 val blockId: Long = if (versionId < LONG_BIT_SPLITTING_VERSION) {
val startLong = index * valuesPerLong
var blockId: Long = if (versionId < LONG_BIT_SPLITTING_VERSION) {
val startLong = index * elementBits / Long.SIZE_BITS
val startOffset = index * elementBits % Long.SIZE_BITS val startOffset = index * elementBits % Long.SIZE_BITS
val endLong = ((index + 1) * elementBits - 1) / Long.SIZE_BITS val endLong = ((index + 1) * elementBits - 1) / Long.SIZE_BITS
@ -55,14 +54,12 @@ class ArrayPaletteData(
data[startLong] ushr startOffset or (data[endLong] shl endOffset) data[startLong] ushr startOffset or (data[endLong] shl endOffset)
} }
} else { } else {
val startLong = index / (Long.SIZE_BITS / elementBits) val startLong = index / valuesPerLong
val startOffset = index % (Long.SIZE_BITS / elementBits) * elementBits val startOffset = index % valuesPerLong * elementBits
data[startLong] ushr startOffset data[startLong] ushr startOffset
} }
blockId = blockId and individualValueMask.toLong() return blockId.toInt() and singleValueMask
return blockId.toInt()
} }
companion object { companion object {

View File

@ -14,8 +14,9 @@
package de.bixilon.minosoft.gui.rendering.gui.hud package de.bixilon.minosoft.gui.rendering.gui.hud
import de.bixilon.kutil.cast.CastUtil.unsafeCast import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.collections.CollectionUtil.synchronizedMapOf import de.bixilon.kutil.collections.CollectionUtil.lockMapOf
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedMap import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedMap
import de.bixilon.kutil.collections.map.LockMap
import de.bixilon.minosoft.config.key.KeyActions import de.bixilon.minosoft.config.key.KeyActions
import de.bixilon.minosoft.config.key.KeyBinding import de.bixilon.minosoft.config.key.KeyBinding
import de.bixilon.minosoft.config.key.KeyCodes import de.bixilon.minosoft.config.key.KeyCodes
@ -41,7 +42,7 @@ class HUDManager(
) : GUIElementDrawer, Initializable { ) : GUIElementDrawer, Initializable {
val renderWindow = guiRenderer.renderWindow val renderWindow = guiRenderer.renderWindow
private val hudElements: MutableMap<ResourceLocation, HUDElement> = synchronizedMapOf() private val hudElements: LockMap<ResourceLocation, HUDElement> = lockMapOf()
override var lastTickTime = 0L override var lastTickTime = 0L
@ -72,12 +73,14 @@ class HUDManager(
} }
fun onMatrixChange() { fun onMatrixChange() {
for (element in hudElements.toSynchronizedMap().values) { hudElements.lock.acquire()
for (element in hudElements.values) {
if (element is LayoutedGUIElement<*>) { if (element is LayoutedGUIElement<*>) {
element.element.forceApply() element.element.forceApply()
} }
element.apply() element.apply()
} }
hudElements.lock.release()
} }
override fun init() { override fun init() {
@ -104,7 +107,9 @@ class HUDManager(
} }
fun draw() { fun draw() {
drawElements(hudElements.toSynchronizedMap().values) hudElements.lock.acquire()
drawElements(hudElements.values)
hudElements.lock.release()
} }
operator fun <T : HUDElement> get(hudBuilder: HUDBuilder<T>): T? { operator fun <T : HUDElement> get(hudBuilder: HUDBuilder<T>): T? {

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.gui.rendering.gui.hud.elements.other
import de.bixilon.kotlinglm.vec2.Vec2i import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.minosoft.data.registries.ResourceLocation import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.text.ChatColors import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.data.text.TextComponent import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.elements.Element import de.bixilon.minosoft.gui.rendering.gui.elements.Element
@ -41,7 +42,7 @@ class BreakProgressHUDElement(guiRenderer: GUIRenderer) : Element(guiRenderer),
override fun draw() { override fun draw() {
val breakProgress = breakInteractionHandler.breakProgress val breakProgress = breakInteractionHandler.breakProgress
if (breakProgress <= 0 || breakProgress >= 1.0) { if (breakProgress <= 0 || breakProgress >= 1.0) {
textElement.text = "" textElement.text = ChatComponent.EMPTY
this.percent = -1 this.percent = -1
return return
} }