unifont rasterizer: remove full textures, optimize texture size

This commit is contained in:
Bixilon 2023-06-17 14:57:28 +02:00
parent 638840c63e
commit 82076fd6d7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 18 additions and 5 deletions

View File

@ -21,7 +21,6 @@ import de.bixilon.minosoft.gui.rendering.font.loader.FontLoader
import de.bixilon.minosoft.gui.rendering.font.types.FontType import de.bixilon.minosoft.gui.rendering.font.types.FontType
import de.bixilon.minosoft.gui.rendering.font.types.PostInitFontType import de.bixilon.minosoft.gui.rendering.font.types.PostInitFontType
import de.bixilon.minosoft.gui.rendering.font.types.font.EmptyFont import de.bixilon.minosoft.gui.rendering.font.types.font.EmptyFont
import de.bixilon.minosoft.gui.rendering.font.types.font.Font
import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType import de.bixilon.minosoft.util.logging.LogMessageType
@ -30,6 +29,7 @@ class FontManager(
val default: FontType, val default: FontType,
) { ) {
fun postInit(latch: AbstractLatch) { fun postInit(latch: AbstractLatch) {
if (default is PostInitFontType) { if (default is PostInitFontType) {
default.postInit(latch) default.postInit(latch)
@ -37,7 +37,7 @@ class FontManager(
} }
operator fun get(font: ResourceLocation?): Font? = null operator fun get(font: ResourceLocation?): FontType? = null
companion object { companion object {
fun create(context: RenderContext, latch: AbstractLatch): FontManager { fun create(context: RenderContext, latch: AbstractLatch): FontManager {

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.gui.rendering.font.types.unicode.unihex
import de.bixilon.minosoft.gui.rendering.font.renderer.code.CodePointRenderer import de.bixilon.minosoft.gui.rendering.font.renderer.code.CodePointRenderer
import de.bixilon.minosoft.gui.rendering.system.base.texture.array.StaticTextureArray import de.bixilon.minosoft.gui.rendering.system.base.texture.array.StaticTextureArray
import de.bixilon.minosoft.gui.rendering.system.opengl.texture.OpenGLTextureArray.Companion.TEXTURE_RESOLUTION_ID_MAP
class UnifontRasterizer( class UnifontRasterizer(
private val array: StaticTextureArray, private val array: StaticTextureArray,
@ -26,8 +27,12 @@ class UnifontRasterizer(
fun add(data: ByteArray): CodePointRenderer = add(data.size / (HEIGHT / Byte.SIZE_BITS), data) fun add(data: ByteArray): CodePointRenderer = add(data.size / (HEIGHT / Byte.SIZE_BITS), data)
fun add(width: Int, data: ByteArray): CodePointRenderer { fun add(width: Int, data: ByteArray): CodePointRenderer {
for (texture in textures) { val iterator = textures.iterator()
texture.add(width, data)?.let { return it } for (texture in iterator) {
val code = texture.add(width, data) ?: continue
if (texture.totalRemaining == 0) iterator.remove()
this.width -= width
return code
} }
val renderer = createTexture().add(width, data)!! val renderer = createTexture().add(width, data)!!
this.width -= width this.width -= width
@ -35,7 +40,13 @@ class UnifontRasterizer(
} }
private fun calculateRows(width: Int): Int { private fun calculateRows(width: Int): Int {
return 1024 / 16 for (index in TEXTURE_RESOLUTION_ID_MAP.size - 1 downTo 0) {
val resolution = TEXTURE_RESOLUTION_ID_MAP[index]
val size = resolution * resolution / HEIGHT
if (width >= size) return resolution / HEIGHT
}
return 1
} }
private fun createTexture(): UnifontTexture { private fun createTexture(): UnifontTexture {

View File

@ -44,6 +44,7 @@ class UnifontTexture(
override val state: TextureStates = TextureStates.LOADED override val state: TextureStates = TextureStates.LOADED
private val remaining = IntArray(rows) { size.x } private val remaining = IntArray(rows) { size.x }
var totalRemaining = size.x * rows
override fun load(context: RenderContext) = Unit override fun load(context: RenderContext) = Unit
@ -51,6 +52,7 @@ class UnifontTexture(
for ((index, remaining) in remaining.withIndex()) { for ((index, remaining) in remaining.withIndex()) {
if (remaining < width) continue if (remaining < width) continue
this.remaining[index] = remaining - width this.remaining[index] = remaining - width
totalRemaining -= width
return rasterize(index, size.x - remaining, width, data) return rasterize(index, size.x - remaining, width, data)
} }