diff --git a/CHANGELOG.md b/CHANGELOG.md index cb5a10c..c65187b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] + +- Fixed gradients and images that are only drawn once, such as the colour picker's gradients while a colour is dragged, gradually using up texture space shared by the UI and HUDs, which made later drawing slower and used more memory + ## [3.1.2] - Item icons on 1.21.8 and newer are drawn by vanilla's own GUI item renderer, so animated and enchanted items, oversized models such as tridents, and resource pack item models look the same as in vanilla menus diff --git a/core/src/main/kotlin/org/polyfrost/compose/render/PolyGlyphPage.kt b/core/src/main/kotlin/org/polyfrost/compose/render/PolyGlyphPage.kt index f73efc4..bfa98ac 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/render/PolyGlyphPage.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/render/PolyGlyphPage.kt @@ -1,6 +1,7 @@ package org.polyfrost.compose.render import java.nio.IntBuffer +import java.util.Collections import java.util.WeakHashMap import java.util.concurrent.CompletableFuture import java.util.concurrent.ConcurrentHashMap @@ -753,6 +754,7 @@ public object PolyGlyphPage { private val bitmaps = WeakHashMap() private val placedLineages = WeakHashMap() + private val seenOnce = Collections.newSetFromMap(WeakHashMap()) private var bitmapArea = 0 private var placedSince = 0L private var placed = 0 @@ -763,14 +765,19 @@ public object PolyGlyphPage { if (b.width > BITMAP_MAX || b.height > BITMAP_MAX) return null val lineage = b.lineage if (lineage != null && placedLineages.containsKey(lineage)) return null - val now = System.nanoTime() - if (now - placedSince > 1_000_000_000L) { placedSince = now; placed = 0 } - if (++placed > 128) return null val area = (b.width + 2) * (b.height + 2) if (bitmapArea + area > BITMAP_AREA) { bitmapArea = bitmapArea() if (bitmapArea + area > BITMAP_AREA) return null } + // only bitmaps drawn again get a slot, so one-offs (such as a ramp per frame while a colour is dragged) don't use up the atlas + if (!seenOnce.remove(b)) { + seenOnce += b + return null + } + val now = System.nanoTime() + if (now - placedSince > 1_000_000_000L) { placedSince = now; placed = 0 } + if (++placed > 128) return null val g = current().place(b) ?: newPage().place(b) ?: return null bitmapArea += area bitmaps[b] = g @@ -778,6 +785,11 @@ public object PolyGlyphPage { return g } + /** Whether [b] is due a slot when next drawn, so recordings that drew it from its own texture are worth redrawing. */ + @ApiStatus.Internal + @JvmStatic + public fun pending(b: PolyBitmap): Boolean = b in seenOnce + private fun bitmapArea(): Int { var sum = 0 for (g in bitmaps.values) sum += (g.w + 2) * (g.h + 2) diff --git a/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt b/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt index 0d7e434..5b78525 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt @@ -478,8 +478,9 @@ public abstract class PolyTessellator : PolyRenderer { val k = deviceScale() val tex = fill?.bitmap ?: if (gradient.linear) ramp(gradient) else boxRaster(gradient, w / h, w * k, h * k) if (cpuOnly) return - // The atlas packs other textures next to the ramp, so a ramp read past its ends needs a texture of its own to clamp - val g = if (fill != null || gradient.linear && !inRamp(gradient, x, y, w, h)) null else atlasBitmap(tex) + // The atlas packs other textures next to the ramp, so a ramp read past its ends needs a texture of its own to clamp; it's still + // asked for, or a slot left pending by an earlier draw would keep recordings of this one redrawing + val g = if (fill != null) null else atlasBitmap(tex)?.takeUnless { gradient.linear && !inRamp(gradient, x, y, w, h) } begin(g?.page ?: bitmapTexture(tex), false) val s = state shadeSx = s.sx; shadeSy = s.sy; shadeTx = s.tx; shadeTy = s.ty diff --git a/core/src/test/kotlin/org/polyfrost/compose/render/PolyGlyphPageTest.kt b/core/src/test/kotlin/org/polyfrost/compose/render/PolyGlyphPageTest.kt index cc9b37e..079c01b 100644 --- a/core/src/test/kotlin/org/polyfrost/compose/render/PolyGlyphPageTest.kt +++ b/core/src/test/kotlin/org/polyfrost/compose/render/PolyGlyphPageTest.kt @@ -1,6 +1,8 @@ package org.polyfrost.compose.render import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Assertions.assertSame import org.junit.jupiter.api.Assertions.assertTrue @@ -32,10 +34,30 @@ class PolyGlyphPageTest { val atlas = PolyGlyphPage.atlas(face, 4000) repeat(5) { PolyGlyphPage.glyph(atlas, face, 4000, it) } val bitmap = PolyBitmap(200, 200, ByteArray(200 * 200 * 4)) + PolyGlyphPage.bitmap(bitmap) val g = PolyGlyphPage.bitmap(bitmap) assertTrue(g != null && g.page === PolyGlyphPage.pages.last()) } + @Test + fun oneOffBitmapsLeaveTheAtlasAlone() { + val revision = PolyGlyphPage.revision + repeat(200) { assertNull(PolyGlyphPage.bitmap(PolyBitmap(256, 1, ByteArray(256 * 4)))) } + assertEquals(revision, PolyGlyphPage.revision) + } + + @Test + fun bitmapsDrawnAgainGetAndKeepASlot() { + val bitmap = PolyBitmap(4, 4, ByteArray(64)) + assertFalse(PolyGlyphPage.pending(bitmap)) + assertNull(PolyGlyphPage.bitmap(bitmap)) + assertTrue(PolyGlyphPage.pending(bitmap)) + val g = PolyGlyphPage.bitmap(bitmap) + assertNotNull(g) + assertFalse(PolyGlyphPage.pending(bitmap)) + repeat(3) { assertSame(g, PolyGlyphPage.bitmap(bitmap)) } + } + @Test fun blankGlyphsAreRasterizedOnce() { val face = CountingFace { null } diff --git a/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt b/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt index 3205212..8bdeafe 100644 --- a/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt +++ b/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt @@ -39,6 +39,12 @@ class PolyTessellatorTest { fun done() = also { flush() }.batches } + // Bitmaps only get an atlas slot once drawn again, so this draws a frame ahead to see what later frames look like + private fun secondFrame(draw: Recorder.() -> Unit): List { + Recorder().draw() + return Recorder().apply(draw).done() + } + private class Group(val items: List, val alpha: Float) private class GlyphRecorder : PolyTessellator() { @@ -109,7 +115,7 @@ class PolyTessellatorTest { @Test fun nearestImageUvsSitOffTexelTies() { val bmp = PolyBitmap(16, 16, ByteArray(16 * 16 * 4) { -1 }) - fun firstVertex(q: FilterQuality) = Recorder().run { image(bmp, 3f, 5f, 52f, 52f, 0f, 0f, 1f, 1f, -1, q); done() }.single().verts + fun firstVertex(q: FilterQuality) = secondFrame { image(bmp, 3f, 5f, 52f, 52f, 0f, 0f, 1f, 1f, -1, q) }.single().verts val linear = firstVertex(FilterQuality.Low) val nearest = firstVertex(FilterQuality.None) val texelU = abs(linear[10] - linear[2]) / 16f @@ -527,9 +533,7 @@ class PolyTessellatorTest { @Test fun linearGradientMapsPositionToTheRamp() { - val r = Recorder() - r.rectGradient(0f, 0f, 100f, 10f, PolyGradient.horizontal(0xFFFF0000.toInt(), 0xFF0000FF.toInt()), 0f) - val b = r.done().single() + val b = secondFrame { rectGradient(0f, 0f, 100f, 10f, PolyGradient.horizontal(0xFFFF0000.toInt(), 0xFF0000FF.toInt()), 0f) }.single() val page = b.texture as PolyGlyphPage.Page val s = PolyGlyphPage.SIZE val left = b.verts[2] * s - (0.5f + 255f * b.verts[0] / 100f) @@ -549,13 +553,13 @@ class PolyTessellatorTest { @Test fun gradientPathsSampleTheRampAndBatchWithShapes() { PolyCompose.install() - val r = Recorder() - val canvas = PolyCanvas(r) val gradient = PolyPaint().apply { shader = LinearGradientShader(Offset.Zero, Offset(100f, 0f), listOf(Color.Red, Color.Blue)) } - canvas.drawPath(Path().apply { moveTo(0f, 0f); lineTo(100f, 0f); lineTo(0f, 10f); close() }, gradient) - canvas.drawPath(Path().apply { moveTo(10f, 20f); lineTo(90f, 20f) }, gradient.copy().apply { style = PaintingStyle.Stroke; strokeWidth = 2f }) - canvas.drawPath(Path().apply { moveTo(0f, 30f); lineTo(10f, 30f); lineTo(0f, 40f); close() }, PolyPaint()) - val b = r.done().single() + val b = secondFrame { + val canvas = PolyCanvas(this) + canvas.drawPath(Path().apply { moveTo(0f, 0f); lineTo(100f, 0f); lineTo(0f, 10f); close() }, gradient) + canvas.drawPath(Path().apply { moveTo(10f, 20f); lineTo(90f, 20f) }, gradient.copy().apply { style = PaintingStyle.Stroke; strokeWidth = 2f }) + canvas.drawPath(Path().apply { moveTo(0f, 30f); lineTo(10f, 30f); lineTo(0f, 40f); close() }, PolyPaint()) + }.single() val page = b.texture as PolyGlyphPage.Page val s = PolyGlyphPage.SIZE var shaded = 0 @@ -575,6 +579,17 @@ class PolyTessellatorTest { assertTrue(shaded > 0) } + @Test + fun rampsReadPastTheirEndsStillTakeUpTheirPendingSlot() { + PolyCompose.install() + val paint = PolyPaint().apply { shader = LinearGradientShader(Offset(40f, 0f), Offset(60f, 0f), listOf(Color(0xFF102030), Color(0xFF302010))) } + fun draw(from: Float, to: Float) = Recorder().apply { PolyCanvas(this).drawRect(from, 0f, to, 10f, paint) }.done().single().texture + draw(45f, 55f) + val past = draw(0f, 100f) + assertTrue(past is PolyBitmap) + assertFalse(PolyGlyphPage.pending(past as PolyBitmap)) + } + @Test fun strokeGradientFollowsGradientDirectionUnderTransform() { val r = Recorder() @@ -593,9 +608,7 @@ class PolyTessellatorTest { @Test fun shadowIsNineSlicesOfABlurredMask() { - val r = Recorder() - r.shadow(10f, 10f, 40f, 60f, 4f, PolyShadow(PolyColor(0x80000000.toInt()), 6f, 0f, 0f, 0f)) - val b = r.done().single() + val b = secondFrame { shadow(10f, 10f, 40f, 60f, 4f, PolyShadow(PolyColor(0x80000000.toInt()), 6f, 0f, 0f, 0f)) }.single() assertEquals(9 * 4, b.colors.size) assertTrue(b.colors.all { it == 0x80000000.toInt() }) val page = b.texture as PolyGlyphPage.Page @@ -606,15 +619,13 @@ class PolyTessellatorTest { val xs = b.verts.filterIndexed { i, _ -> i % 4 == 0 } assertEquals(30f, (xs.min() + xs.max()) / 2f, 1e-3f) assertTrue(xs.min() < 10f - 6f) - val small = Recorder().apply { shadow(0f, 0f, 4f, 4f, 2f, PolyShadow(PolyColor(0x80000000.toInt()), 6f, 0f, 4f, 0f)) }.done().single() + val small = secondFrame { shadow(0f, 0f, 4f, 4f, 2f, PolyShadow(PolyColor(0x80000000.toInt()), 6f, 0f, 4f, 0f)) }.single() assertEquals(4 * 4, small.colors.size) } @Test fun bigShadowMaskIsDownsampledButKeepsItsExtent() { - val r = Recorder() - r.shadow(0f, 0f, 400f, 300f, 20f, PolyShadow(PolyColor(0x80000000.toInt()), 50f, 0f, 0f, 0f)) - val b = r.done().single() + val b = secondFrame { shadow(0f, 0f, 400f, 300f, 20f, PolyShadow(PolyColor(0x80000000.toInt()), 50f, 0f, 0f, 0f)) }.single() assertEquals(9 * 4, b.colors.size) val page = b.texture as PolyGlyphPage.Page val s = PolyGlyphPage.SIZE @@ -631,8 +642,8 @@ class PolyTessellatorTest { @Test fun shadowWithHoleSkipsOnlyTheInsideOfTheHole() { val sh = PolyShadow(PolyColor(0x80000000.toInt()), 50f, 0f, 0f, 0f) - val full = Recorder().apply { shadow(-4f, 9f, 408f, 308f, 24f, sh) }.done().single() - val holed = Recorder().apply { shadowWithHole(-4f, 9f, 408f, 308f, 24f, sh, 0f, 0f, 400f, 300f, 20f) }.done().single() + val full = secondFrame { shadow(-4f, 9f, 408f, 308f, 24f, sh) }.single() + val holed = secondFrame { shadowWithHole(-4f, 9f, 408f, 308f, 24f, sh, 0f, 0f, 400f, 300f, 20f) }.single() fun hits(b: Batch, x: Float, y: Float): Int { var n = 0 for (q in 0 until b.colors.size / 4) { @@ -679,13 +690,13 @@ class PolyTessellatorTest { @Test fun imageCarriesUvAndTint() { - val r = Recorder() val bmp = PolyBitmap(4, 4, ByteArray(64) { -1 }) - r.saveLayerAlpha(0.5f) - r.image(bmp, 0f, 0f, 8f, 8f, 0.25f, 0f, 0.75f, 1f, 0xFF00FF00.toInt()) - r.restore() - r.rect(0f, 0f, 2f, 2f, -1, 0f) - val b = r.done().single() + val b = secondFrame { + saveLayerAlpha(0.5f) + image(bmp, 0f, 0f, 8f, 8f, 0.25f, 0f, 0.75f, 1f, 0xFF00FF00.toInt()) + restore() + rect(0f, 0f, 2f, 2f, -1, 0f) + }.single() assertTrue(b.texture is PolyGlyphPage.Page) val us = (0 until 4).map { b.verts[it * 4 + 2] * PolyGlyphPage.SIZE }.toSet() assertEquals(2, us.size) @@ -931,6 +942,8 @@ class PolyTessellatorTest { override fun raster(glyph: Int, pixelSize: Float) = PolyGlyphPage.Raster(0, -4, 2, 4, ByteArray(8) { -1 }) } val sheet = PolyBitmap(16, 16, ByteArray(16 * 16 * 4) { -1 }) + // the sheet only gets its atlas slot once drawn again + PolyGlyphPage.bitmap(sheet) val glyph = McGlyph(sheet, 0f, 0f, 0.125f, 0.5f, 0f, 2f, 8f, 6f, 1f) val standIn = McGlyph(sheet, 0f, 0f, 0.25f, 0.5f, 0f, 4f, 8f, 6f, 1f) val rasterizer = PolyGlyphPage.rasterizer @@ -966,6 +979,8 @@ class PolyTessellatorTest { @Test fun nearestMcGlyphsStartOnWholePixelsAndKeepTheirScale() { val sheet = PolyBitmap(16, 16, ByteArray(16 * 16 * 4) { -1 }) + // the sheet only gets its atlas slot once drawn again + PolyGlyphPage.bitmap(sheet) val glyph = McGlyph(sheet, 0f, 0f, 0.5f, 0.5f, 0f, 8f, 8f, 8f, 1f) val source = McGlyphs.source McGlyphs.source = { glyph } @@ -1078,10 +1093,9 @@ class PolyTessellatorTest { @Test fun nearestImagesBatchApart() { val bmp = PolyBitmap(4, 4, ByteArray(64) { -1 }) - val b = Recorder().run { + val b = secondFrame { image(bmp, 0f, 0f, 8f, 8f, FilterQuality.None) image(bmp, 8f, 0f, 8f, 8f) - done() } assertEquals(listOf(true, false), b.map { it.nearest }) assertEquals(b[0].texture, b[1].texture) diff --git a/src/main/kotlin/org/polyfrost/compose/minecraft/render/blaze/BlazeRenderer.kt b/src/main/kotlin/org/polyfrost/compose/minecraft/render/blaze/BlazeRenderer.kt index 29df073..e869836 100644 --- a/src/main/kotlin/org/polyfrost/compose/minecraft/render/blaze/BlazeRenderer.kt +++ b/src/main/kotlin/org/polyfrost/compose/minecraft/render/blaze/BlazeRenderer.kt @@ -166,7 +166,7 @@ class BlazeRenderer(private val guiScale: Float, private val keepVertices: Boole override fun valid(captured: Any): Boolean { val c = captured as Captured val bitmaps = c.bitmaps - for (i in bitmaps.indices) if (BitmapTextures.get(bitmaps[i]) !== c.refs[i]) return false + for (i in bitmaps.indices) if (BitmapTextures.get(bitmaps[i]) !== c.refs[i] || PolyGlyphPage.pending(bitmaps[i])) return false val textures = c.textures ?: LayerOp.texturesOf(c.ops).also { c.textures = it } if (anyIndexed(textures) { it.isClosed }) return false if (c.glyphGeneration != GlyphAtlas.generation) { @@ -2376,7 +2376,7 @@ object BlazeHud { @JvmStatic fun render(gfx: GuiGraphicsExtractor, draw: (stable: RenderContext, volatile: RenderContext) -> Unit) { val guiScale = Minecraft.getInstance().window.guiScale.toFloat() - if (dirty || guiScale != scale || stable.stale() || volatile.stale()) { + if (dirty || guiScale != scale || stable.stale() || volatile.stale() || stable.pending() || volatile.pending()) { scale = guiScale val a = reuse(stableRenderer, guiScale).also { stableRenderer = it } val b = reuse(volatileRenderer, guiScale).also { volatileRenderer = it } @@ -2455,6 +2455,7 @@ internal class Recording(private val coalesce: Boolean = true, private val coale private var raw: List<(GuiGraphicsExtractor) -> Unit> = emptyList() private var stable = 0 private var pin = BitmapTextures.unpinned + private var bitmaps: List = emptyList() private var scale = 0f private var gameFrame = false private var lives: List = emptyList() @@ -2466,7 +2467,7 @@ internal class Recording(private val coalesce: Boolean = true, private val coale fun render(gfx: GuiGraphicsExtractor, dirty: Boolean, draw: (RenderContext) -> Unit) { val guiScale = Minecraft.getInstance().window.guiScale.toFloat() - if (dirty || guiScale != scale || stale()) { + if (dirty || guiScale != scale || stale() || pending()) { scale = guiScale val reused = spare?.takeIf { it.matches(guiScale) } spare = null @@ -2504,6 +2505,7 @@ internal class Recording(private val coalesce: Boolean = true, private val coale collectTextures(raw) pin.release() pin = BitmapTextures.pin(renderer.bitmaps, true) + bitmaps = ArrayList(renderer.bitmaps) } private fun sameOps(a: List<(GuiGraphicsExtractor) -> Unit>, b: List<(GuiGraphicsExtractor) -> Unit>): Boolean { @@ -2531,6 +2533,9 @@ internal class Recording(private val coalesce: Boolean = true, private val coale fun stale(): Boolean = anyIndexed(textures) { it.isClosed } + // bitmaps first drawn from their own textures get an atlas slot once drawn again, which needs a redraw + fun pending(): Boolean = anyIndexed(bitmaps, PolyGlyphPage::pending) + fun idle() { pin.touch() if (coalesce && ++stable == coalesceFrames) ops = BlazeRenderer.coalesce(raw) diff --git a/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlHost.kt b/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlHost.kt index 20f43bd..757898a 100644 --- a/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlHost.kt +++ b/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlHost.kt @@ -692,7 +692,7 @@ object GlHost { backdropFresh = fresh //? if < 1.21.8 //if (drawsGuiOverScreen) glUnderlay?.let { gl.restored { drawGui(it) } } - val reuse = screenFrame?.takeUnless { glScreenDirty } + val reuse = screenFrame?.takeUnless { glScreenDirty || it.stale() } glScreenDirty = false screenFrame = drawGl(fbo, reuse, screenFrame, true, paint) } diff --git a/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlRenderer.kt b/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlRenderer.kt index 9b0e720..3a24562 100644 --- a/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlRenderer.kt +++ b/src/main/kotlin/org/polyfrost/compose/minecraft/render/gl/GlRenderer.kt @@ -1980,7 +1980,8 @@ internal object GlTextures { fun external(id: Int): Tex = external.getOrPut(id) { Tex(null, id) } - fun alive(key: Any): Boolean = key !is Tex || key.bitmap.let { it == null || bitmaps.peek(it)?.texture === key } + // a bitmap pending an atlas slot gets one once drawn again, so batches drawing it from its own texture are worth redrawing + fun alive(key: Any): Boolean = key !is Tex || key.bitmap.let { it == null || bitmaps.peek(it)?.texture === key && !PolyGlyphPage.pending(it) } fun itemAtlas(batches: List): Boolean { for (i in batches.indices) {