lightmap: reduce memory allocations

This commit is contained in:
Moritz Zwerger 2025-03-26 23:37:50 +01:00
parent 9cb58126c7
commit b85c9e475f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 19 additions and 16 deletions

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger * Copyright (C) 2020-2025 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -16,7 +16,6 @@ package de.bixilon.minosoft.gui.rendering.light.updater
import de.bixilon.kotlinglm.vec3.Vec3 import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.minosoft.gui.rendering.light.LightmapBuffer import de.bixilon.minosoft.gui.rendering.light.LightmapBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import java.util.*
object FullbrightLightUpdater : LightmapUpdater { object FullbrightLightUpdater : LightmapUpdater {
@ -25,7 +24,6 @@ object FullbrightLightUpdater : LightmapUpdater {
return return
} }
val random = Random(10000L)
for (sky in 0 until ProtocolDefinition.LIGHT_LEVELS) { for (sky in 0 until ProtocolDefinition.LIGHT_LEVELS) {
for (block in 0 until ProtocolDefinition.LIGHT_LEVELS) { for (block in 0 until ProtocolDefinition.LIGHT_LEVELS) {
buffer[sky, block] = Vec3(1.0f) buffer[sky, block] = Vec3(1.0f)

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2024 Moritz Zwerger * Copyright (C) 2020-2025 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -26,7 +26,7 @@ import de.bixilon.minosoft.data.world.weather.WorldWeather
import de.bixilon.minosoft.gui.rendering.light.LightmapBuffer import de.bixilon.minosoft.gui.rendering.light.LightmapBuffer
import de.bixilon.minosoft.gui.rendering.light.updater.LightmapUpdater import de.bixilon.minosoft.gui.rendering.light.updater.LightmapUpdater
import de.bixilon.minosoft.gui.rendering.sky.SkyRenderer import de.bixilon.minosoft.gui.rendering.sky.SkyRenderer
import de.bixilon.minosoft.gui.rendering.util.VecUtil.clamp import de.bixilon.minosoft.gui.rendering.util.VecUtil.clampAssign
import de.bixilon.minosoft.gui.rendering.util.VecUtil.modify import de.bixilon.minosoft.gui.rendering.util.VecUtil.modify
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.interpolateLinear import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.interpolateLinear
import de.bixilon.minosoft.protocol.network.session.play.PlaySession import de.bixilon.minosoft.protocol.network.session.play.PlaySession
@ -91,9 +91,10 @@ class NormalLightmapUpdater(
val gamma = profile.gamma val gamma = profile.gamma
val nightVision = getNightVisionStrength(millis) val nightVision = getNightVisionStrength(millis)
var color = Vec3()
for (sky in 0 until ProtocolDefinition.LIGHT_LEVELS) { for (sky in 0 until ProtocolDefinition.LIGHT_LEVELS) {
for (block in 0 until ProtocolDefinition.LIGHT_LEVELS) { for (block in 0 until ProtocolDefinition.LIGHT_LEVELS) {
var color = combine(skyColors[sky], blockColors[block]) combine(skyColors[sky], blockColors[block], color)
color = tweak(color, gamma, dimension.effects.brighten, nightVision) color = tweak(color, gamma, dimension.effects.brighten, nightVision)
buffer[sky, block] = color buffer[sky, block] = color
} }
@ -165,10 +166,10 @@ class NormalLightmapUpdater(
} }
private fun combine(sky: Vec3, block: Vec3): Vec3 { private fun combine(sky: Vec3, block: Vec3, output: Vec3) {
val color = sky + block Vec3.plus(output, sky, block.x, block.y, block.z)
return color.clamp() output.clampAssign()
} }
private fun tweak( private fun tweak(
@ -200,7 +201,7 @@ class NormalLightmapUpdater(
} }
private fun applyBrighten(color: Vec3, brighten: Vec3): Vec3 { private fun applyBrighten(color: Vec3, brighten: Vec3): Vec3 {
return interpolateLinear(0.25f, color, brighten).clamp() return interpolateLinear(0.25f, color, brighten).apply { clampAssign() }
} }
private fun applyGamma(color: Vec3, gamma: Float): Vec3 { private fun applyGamma(color: Vec3, gamma: Float): Vec3 {
@ -225,8 +226,8 @@ class NormalLightmapUpdater(
return this * (1.0f - value) + value return this * (1.0f - value) + value
} }
private fun Vec3.clamp(): Vec3 { private fun Vec3.clampAssign() {
return clamp(0.0f, 1.0f) this.clampAssign(0.0f, 1.0f)
} }
private companion object { private companion object {

View File

@ -101,6 +101,8 @@ class ParticleRenderer(
profile::maxAmount.observe(this, true) { maxAmount = minOf(it, MAXIMUM_AMOUNT) } profile::maxAmount.observe(this, true) { maxAmount = minOf(it, MAXIMUM_AMOUNT) }
profile::enabled.observe(this, true) { enabled = it } profile::enabled.observe(this, true) { enabled = it }
// TODO: unload particles when renderer is paused
session.events.listen<CameraMatrixChangeEvent> { matrixUpdate = true } session.events.listen<CameraMatrixChangeEvent> { matrixUpdate = true }
mesh.load() mesh.load()

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.gui.rendering.particle package de.bixilon.minosoft.gui.rendering.particle
import de.bixilon.kutil.concurrent.schedule.RepeatedTask
import de.bixilon.kutil.exception.ExceptionUtil.ignoreAll import de.bixilon.kutil.exception.ExceptionUtil.ignoreAll
import de.bixilon.kutil.time.TimeUtil.millis import de.bixilon.kutil.time.TimeUtil.millis
import de.bixilon.minosoft.data.world.chunk.ChunkUtil.isInViewDistance import de.bixilon.minosoft.data.world.chunk.ChunkUtil.isInViewDistance
@ -24,7 +23,6 @@ import de.bixilon.minosoft.protocol.network.session.play.PlaySessionStates
class ParticleTicker(val renderer: ParticleRenderer) { class ParticleTicker(val renderer: ParticleRenderer) {
private val particles = renderer.particles private val particles = renderer.particles
private val context = renderer.context private val context = renderer.context
private var task: RepeatedTask? = null
private fun canTick(): Boolean { private fun canTick(): Boolean {
@ -69,8 +67,7 @@ class ParticleTicker(val renderer: ParticleRenderer) {
if (collect) { if (collect) {
particle.addVertex(renderer.mesh, renderer.translucentMesh, time) particle.addVertex(renderer.mesh, renderer.translucentMesh, time)
} }
if (index % 1000 == 0) { if (index % 1000 == 0) { // don't spam the os with time calls
// check periodically if time is exceeded
time = millis() time = millis()
if (time - start > MAX_TICK_TIME) { if (time - start > MAX_TICK_TIME) {
break break

View File

@ -169,6 +169,11 @@ object VecUtil {
z = horizontal(positionHash shr 8)).clamp(-maxModelOffset, maxModelOffset) z = horizontal(positionHash shr 8)).clamp(-maxModelOffset, maxModelOffset)
} }
fun Vec3.clampAssign(min: Float, max: Float) {
this.x = x.clamp(min, max)
this.y = y.clamp(min, max)
this.z = z.clamp(min, max)
}
fun Vec3.clamp(min: Float, max: Float): Vec3 { fun Vec3.clamp(min: Float, max: Float): Vec3 {
return Vec3( return Vec3(
x = x.clamp(min, max), x = x.clamp(min, max),