Skip to content

Commit 89304bd

Browse files
committed
cleanup and improve font rendering
1 parent 70e3917 commit 89304bd

File tree

1 file changed

+28
-38
lines changed
  • modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/internal

1 file changed

+28
-38
lines changed

modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/internal/GLRendererImpl.kt

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import dev.deftu.omnicore.internal.client.render.shader.ShaderInternals
77
import org.apache.logging.log4j.LogManager
88
import org.lwjgl.BufferUtils
99
import org.lwjgl.opengl.GL11.*
10-
import org.lwjgl.opengl.GL13
10+
import org.lwjgl.opengl.GL13.*
1111
import org.lwjgl.opengl.GL14.*
1212
import org.lwjgl.opengl.GL15.*
1313
import org.lwjgl.opengl.GL20.*
@@ -25,7 +25,6 @@ import org.polyfrost.polyui.unit.Vec4
2525
import org.polyfrost.polyui.utils.toDirectByteBuffer
2626
import org.polyfrost.polyui.utils.toDirectByteBufferNT
2727
import java.nio.ByteBuffer
28-
import java.nio.ByteOrder
2928
import java.nio.FloatBuffer
3029
import kotlin.math.cos
3130
import kotlin.math.roundToInt
@@ -35,10 +34,9 @@ import kotlin.math.tan
3534
private val LOGGER = LogManager.getLogger("PolyUI/GLRenderer")
3635

3736
private const val MAX_UI_DEPTH = 16
38-
private const val FONT_MAX_BITMAP_W = 1024
37+
private const val FONT_MAX_BITMAP_W = 1536
3938
private const val FONT_MAX_BITMAP_H = 512
4039
private const val ATLAS_SIZE = 2048
41-
private const val FONT_RENDER_SIZE = 24f // 48f scales nicely to 16f, 12f, 32f, etc.
4240
private const val ATLAS_SVG_UPSCALE_FACTOR = 2f
4341
private const val STRIDE = 4 + 4 + 4 + 4 + 4 + 1 // bounds, radii, color0, color1, UV, thick
4442
private const val MAX_BATCH = 1024
@@ -73,7 +71,8 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
7371
private val buffer = BufferUtils.createFloatBuffer(MAX_BATCH * STRIDE)
7472
private val scissorStack = IntArray(MAX_UI_DEPTH * 4)
7573
private val transformStack = Array(MAX_UI_DEPTH) { FloatArray(9) }
76-
private val fonts = HashMap<Font, FontAtlas>()
74+
private val transformBuffer = BufferUtils.createFloatBuffer(9)
75+
private val fonts = HashMap<Int, FontAtlas>()
7776
private val init get() = program != 0
7877

7978
// lateinit
@@ -103,11 +102,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
103102
private var curTex = 0
104103
private var transformDepth = 0
105104
private var curScissor = 0
106-
private var transform = floatArrayOf(
107-
1f, 0f, 0f,
108-
0f, 1f, 0f,
109-
0f, 0f, 1f
110-
)
105+
private var transform = IDENTITY.copyOf()
111106
private var viewportWidth = 0f
112107
private var viewportHeight = 0f
113108
private var pixelRatio = 1f
@@ -311,9 +306,10 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
311306

312307
@Suppress("SameParameterValue")
313308
private fun glUniformMatrix3fv(location: Int, transpose: Boolean, array: FloatArray) {
314-
val buffer = ByteBuffer.allocateDirect(array.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer().put(array)
315-
buffer.flip()
316-
ShaderInternals.uniformMatrix3(location, transpose, buffer)
309+
val buf = transformBuffer
310+
buf.clear()
311+
buf.put(array).flip()
312+
ShaderInternals.uniformMatrix3(location, transpose, buf)
317313
}
318314

319315
override fun init() {
@@ -465,8 +461,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
465461
popFlushNeeded = false
466462
}
467463

468-
@Suppress("RemoveRedundantQualifierName")
469-
GL13.glActiveTexture(GL_TEXTURE0)
464+
glActiveTexture(GL_TEXTURE0)
470465
glBindTexture(GL_TEXTURE_2D, curTex)
471466

472467
// Quad attrib
@@ -496,8 +491,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
496491
if (prevDepth) glEnable(GL_DEPTH_TEST)
497492
if (prevCull) glEnable(GL_CULL_FACE)
498493
glUseProgram(prevProg)
499-
@Suppress("RemoveRedundantQualifierName")
500-
GL13.glActiveTexture(prevActive)
494+
glActiveTexture(prevActive)
501495
glBindTexture(GL_TEXTURE_2D, prevTex)
502496
glBindBuffer(GL_ARRAY_BUFFER, prevBuf)
503497
if (GlCapabilities.isGl3Available) org.lwjgl.opengl.GL30.glBindVertexArray(prevVao)
@@ -613,7 +607,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
613607
}
614608

615609
override fun text(font: Font, x: Float, y: Float, text: String, color: Color, fontSize: Float) {
616-
val fAtlas = getFontAtlas(font)
610+
val fAtlas = getFontAtlas(font, fontSize)
617611
if (count >= MAX_BATCH) flush()
618612
if (count > 0 && curTex != atlas) flush()
619613
curTex = atlas
@@ -645,7 +639,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
645639
}
646640

647641
override fun textBounds(font: Font, text: String, fontSize: Float): Vec2 {
648-
return getFontAtlas(font).measure(text, fontSize)
642+
return getFontAtlas(font, fontSize).measure(text, fontSize)
649643
}
650644

651645
override fun line(x1: Float, y1: Float, x2: Float, y2: Float, color: Color, width: Float) {
@@ -825,8 +819,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
825819

826820
val prevActive = glGetInteger(GL_ACTIVE_TEXTURE)
827821
val prevTex = glGetInteger(GL_TEXTURE_BINDING_2D)
828-
@Suppress("RemoveRedundantQualifierName")
829-
GL13.glActiveTexture(GL_TEXTURE0)
822+
glActiveTexture(GL_TEXTURE0)
830823
glBindTexture(GL_TEXTURE_2D, atlas)
831824
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
832825
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
@@ -838,8 +831,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
838831
2 -> org.lwjgl.opengl.EXTFramebufferObject.glGenerateMipmapEXT(GL_TEXTURE_2D)
839832
}
840833
glBindTexture(GL_TEXTURE_2D, prevTex)
841-
@Suppress("RemoveRedundantQualifierName")
842-
GL13.glActiveTexture(prevActive)
834+
glActiveTexture(prevActive)
843835

844836
if (image.type == PolyImage.Type.Raster) stb.image_free(d)
845837

@@ -868,25 +860,29 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
868860
}
869861
}
870862

871-
private fun getFontAtlas(font: Font): FontAtlas {
872-
return fonts.getOrPut(font) {
863+
private fun getFontAtlas(font: Font, fontSize: Float): FontAtlas {
864+
val renderSize = when(fontSize) {
865+
in 0f..36f -> 24f
866+
else -> 48f
867+
}
868+
return fonts.getOrPut(font.resourcePath.hashCode() + renderSize.toInt()) {
873869
val data = font.load {
874870
LOGGER.error("Failed to load font: $font", it)
875-
return@getOrPut fonts[PolyUI.defaultFonts.regular]
871+
return@getOrPut fonts[PolyUI.defaultFonts.regular.resourcePath.hashCode() + renderSize.toInt()]
876872
?: throw IllegalStateException("Default font couldn't be loaded")
877873
}.toDirectByteBuffer()
878-
FontAtlas(data, FONT_RENDER_SIZE)
874+
FontAtlas(data, renderSize)
879875
}
880876
}
881877

882878
private var i = 0
883-
fun dumpTexture(texId: Int = atlas) {
879+
override fun dumpAtlas() {
884880
val buf = BufferUtils.createByteBuffer(ATLAS_SIZE * ATLAS_SIZE * 4)
885-
glBindTexture(GL_TEXTURE_2D, texId)
881+
glBindTexture(GL_TEXTURE_2D, atlas)
886882
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf)
887883
glBindTexture(GL_TEXTURE_2D, 0)
888884
stb.image_write_png(
889-
"debug_texture$texId($i).png",
885+
"debug_atlas$i.png",
890886
ATLAS_SIZE,
891887
ATLAS_SIZE,
892888
4,
@@ -896,10 +892,6 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
896892
i++
897893
}
898894

899-
override fun dumpAtlas() {
900-
dumpTexture(atlas)
901-
}
902-
903895
override fun cleanup() {
904896
// dumpAtlas()
905897
if (program != 0) glDeleteProgram(program)
@@ -1010,8 +1002,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
10101002

10111003
val prevActive = glGetInteger(GL_ACTIVE_TEXTURE)
10121004
val prevTex = glGetInteger(GL_TEXTURE_BINDING_2D)
1013-
@Suppress("RemoveRedundantQualifierName")
1014-
GL13.glActiveTexture(GL_TEXTURE0)
1005+
glActiveTexture(GL_TEXTURE0)
10151006
glBindTexture(GL_TEXTURE_2D, atlas)
10161007
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
10171008
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
@@ -1034,8 +1025,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
10341025
2 -> org.lwjgl.opengl.EXTFramebufferObject.glGenerateMipmapEXT(GL_TEXTURE_2D)
10351026
}
10361027
glBindTexture(GL_TEXTURE_2D, prevTex)
1037-
@Suppress("RemoveRedundantQualifierName")
1038-
GL13.glActiveTexture(prevActive)
1028+
glActiveTexture(prevActive)
10391029

10401030
slotX += totalSizeX + 1
10411031
atlasRowHeight = maxOf(atlasRowHeight, totalSizeY + 1)

0 commit comments

Comments
 (0)