From 952f245e8f021f8fe0f0ac1c1058f06f769222c2 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 28 Mar 2023 22:38:27 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=B2=D1=83=D1=85=D1=83=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BD=D0=B5=D0=B2=D1=8B=D0=B9=20=D0=BA=D0=B5=D1=88=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BA=D1=81=D1=82=D1=83=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kstarbound/client/gl/GLStateTracker.kt | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/client/gl/GLStateTracker.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/client/gl/GLStateTracker.kt index 5ef72df3..b2aacdfe 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/client/gl/GLStateTracker.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/client/gl/GLStateTracker.kt @@ -6,7 +6,6 @@ import org.apache.logging.log4j.LogManager import org.lwjgl.opengl.GL import org.lwjgl.opengl.GL46.* import org.lwjgl.opengl.GLCapabilities -import ru.dbotthepony.kstarbound.api.ISBFileLocator import ru.dbotthepony.kstarbound.client.StarboundClient import ru.dbotthepony.kstarbound.client.freetype.FreeType import ru.dbotthepony.kstarbound.client.freetype.InvalidArgumentException @@ -28,10 +27,8 @@ import java.lang.ref.Cleaner import java.lang.ref.WeakReference import java.time.Duration import java.util.* -import java.util.concurrent.ThreadFactory import java.util.concurrent.locks.LockSupport import kotlin.collections.ArrayList -import kotlin.collections.HashMap import kotlin.math.roundToInt import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty @@ -435,22 +432,27 @@ class GLStateTracker(val client: StarboundClient) { fun newVAO() = VertexArrayObject(this) fun newTexture(name: String = "") = GLTexture2D(this, name) - /** - * Так как текстуры не занимают память в куче JVM, а только видеопамять, то - * такой кеш довольно хрупкий - */ - private val named2DTextures: Cache = Caffeine.newBuilder() + // минимальное время хранения 5 минут и... + private val named2DTextures0: Cache = Caffeine.newBuilder() .softValues() .expireAfterAccess(Duration.ofMinutes(5)) .build() + // ...бесконечное хранение пока кто-то все ещё использует текстуру + private val named2DTextures1: Cache = Caffeine.newBuilder() + .weakValues() + .build() + init { - val ref = WeakReference(named2DTextures) + val ref0 = WeakReference(named2DTextures0) + val ref1 = WeakReference(named2DTextures1) val worker = Runnable { while (true) { - val get = ref.get() ?: break - get.cleanUp() + val get0 = ref0.get() ?: break + get0.cleanUp() + val get1 = ref1.get() ?: break + get1.cleanUp() LockSupport.parkNanos(1_000_000_000L) } } @@ -473,14 +475,16 @@ class GLStateTracker(val client: StarboundClient) { fun loadTexture(path: String): GLTexture2D { ensureSameThread() - return named2DTextures.get(path) { - if (!client.starbound.exists(path)) { - LOGGER.error("Texture {} is missing! Falling back to {}", path, missingTexturePath) - missingTexture - } else { - newTexture(path).upload(client.starbound.imageData(path)).generateMips().also { - it.textureMinFilter = GL_NEAREST - it.textureMagFilter = GL_NEAREST + return named2DTextures0.get(path) { + named2DTextures1.get(it) { + if (!client.starbound.exists(it)) { + LOGGER.error("Texture {} is missing! Falling back to {}", it, missingTexturePath) + missingTexture + } else { + newTexture(it).upload(client.starbound.imageData(it)).generateMips().also { + it.textureMinFilter = GL_NEAREST + it.textureMagFilter = GL_NEAREST + } } } }