more optimisation

This commit is contained in:
minjaesong
2022-10-06 21:42:46 +09:00
parent 93ac1011e9
commit 8533f92274
3 changed files with 51 additions and 22 deletions

View File

@@ -25,10 +25,11 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
}
// getters
fun getR(x: Int, y: Int) = array.getFloat(toAddr(x, y))
fun getG(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 1)
fun getB(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 2)
fun getA(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 3)
// fun getR(x: Int, y: Int) = array.getFloat(toAddr(x, y))
// fun getG(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 1)
// fun getB(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 2)
// fun getA(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 3)
// operator fun get(i: Long) = array.getFloat(i)
/**
* Returns a copy of the vector. Use [setVec] to modify the value in the CvecArray
*/
@@ -41,6 +42,31 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
array.getFloat(a + 3)
)
}
/**
* `getAndSet(cvec, x, y)` is equivalent to
* `cvec.set(this.getVec(x, y))`
*/
fun getAndSet(target: Cvec, x: Int, y: Int) {
val a = toAddr(x, y)
target.r = array.getFloat(a + 0)
target.g = array.getFloat(a + 1)
target.b = array.getFloat(a + 2)
target.a = array.getFloat(a + 3)
}
/**
* `getAndSet(cvec, x, y, func)` is equivalent to
* `target.setVec(x, y, func(this.getVec(x, y)))`
*
* The target must have the same dimension as this CvecArray.
*/
fun getAndSetMap(target: UnsafeCvecArray, x: Int, y: Int, transform: (Float) -> Float) {
val a = toAddr(x, y)
target.array.setFloat(a + 0, transform(this.array.getFloat(a + 0)))
target.array.setFloat(a + 1, transform(this.array.getFloat(a + 1)))
target.array.setFloat(a + 2, transform(this.array.getFloat(a + 2)))
target.array.setFloat(a + 3, transform(this.array.getFloat(a + 3)))
}
/**
* @param channel 0 for R, 1 for G, 2 for B, 3 for A
*/
@@ -48,10 +74,11 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
// setters
fun zerofill() = array.fillWith(0)
// fun setR(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y), value) }
// fun setR(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y), value) }
// fun setG(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y) + 1, value) }
// fun setB(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y) + 2, value) }
// fun setA(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y) + 3, value) }
// operator fun set(i: Long, value: Float) = array.setFloat(i, value)
fun setVec(x: Int, y: Int, value: Cvec) {
val a = toAddr(x, y)
array.setFloat(a + 0, value.r)

View File

@@ -30,6 +30,9 @@ import net.torvald.terrarum.modulebasegame.gameactors.*
import net.torvald.terrarum.modulebasegame.gameactors.physicssolver.CollisionSolver
import net.torvald.terrarum.modulebasegame.gameitems.PickaxeCore
import net.torvald.terrarum.modulebasegame.gameworld.GameEconomy
import net.torvald.terrarum.modulebasegame.serialise.LoadSavegame
import net.torvald.terrarum.modulebasegame.serialise.ReadActor
import net.torvald.terrarum.modulebasegame.serialise.WriteSavegame
import net.torvald.terrarum.modulebasegame.ui.*
import net.torvald.terrarum.modulebasegame.worldgenerator.RoguelikeRandomiser
import net.torvald.terrarum.modulebasegame.worldgenerator.Worldgen
@@ -38,9 +41,6 @@ import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.savegame.VDUtil
import net.torvald.terrarum.savegame.VirtualDisk
import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.modulebasegame.serialise.LoadSavegame
import net.torvald.terrarum.modulebasegame.serialise.ReadActor
import net.torvald.terrarum.modulebasegame.serialise.WriteSavegame
import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.ui.UIAutosaveNotifier
import net.torvald.terrarum.ui.UICanvas
@@ -1064,21 +1064,21 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
if (it is CuedByTerrainChange) {
terrainChangeQueue.forEach { cue ->
printdbg(this, "Ingame actors terrainChangeCue: ${cue}")
// printdbg(this, "Ingame actors terrainChangeCue: ${cue}")
it.updateForTerrainChange(cue)
}
}
if (it is CuedByWallChange) {
wallChangeQueue.forEach { cue ->
printdbg(this, "Ingame actors wallChangeCue: ${cue}")
// printdbg(this, "Ingame actors wallChangeCue: ${cue}")
it.updateForWallChange(cue)
}
}
if (it is CuedByWireChange) {
wireChangeQueue.forEach { cue ->
printdbg(this, "Ingame actors wireChangeCue: ${cue}")
// printdbg(this, "Ingame actors wireChangeCue: ${cue}")
it.updateForWireChange(cue)
}
}

View File

@@ -499,7 +499,9 @@ object LightmapRenderer {
// blend shade
_mapThisTileOpacity.max(lx, ly, shadowMap[LandUtil.getBlockAddr(world, worldX, worldY)] ?: colourNull)
_mapThisTileOpacity2.setVec(lx, ly, _mapThisTileOpacity.getVec(lx, ly).mul(1.41421356f))
// _mapThisTileOpacity2.setVec(lx, ly, _mapThisTileOpacity.getVec(lx, ly).mul(1.41421356f))
_mapThisTileOpacity.getAndSetMap(_mapThisTileOpacity2, lx, ly) { it * 1.41421356f }
// open air || luminous tile backed by sunlight
@@ -549,19 +551,19 @@ object LightmapRenderer {
private var swipeY = -1
private var swipeDiag = false
private val distFromLightSrc = Ivec4()
private fun _swipeTask(x: Int, y: Int, x2: Int, y2: Int, lightmap: UnsafeCvecArray, distFromLightSrc: Ivec4) {
private fun _swipeTask(x: Int, y: Int, x2: Int, y2: Int, lightmap: UnsafeCvecArray) {//, distFromLightSrc: Ivec4) {
if (x2 < 0 || y2 < 0 || x2 >= LIGHTMAP_WIDTH || y2 >= LIGHTMAP_HEIGHT) return
_ambientAccumulator.set(_mapLightLevelThis.getVec(x, y))
// _ambientAccumulator.set(_mapLightLevelThis.getVec(x, y))
_mapLightLevelThis.getAndSet(_ambientAccumulator, x, y)
if (!swipeDiag) {
_thisTileOpacity.set(_mapThisTileOpacity.getVec(x, y))
_ambientAccumulator.maxAndAssign(darkenColoured(x2, y2, _thisTileOpacity, lightmap, distFromLightSrc))
_mapThisTileOpacity.getAndSet(_thisTileOpacity, x, y)
_ambientAccumulator.maxAndAssign(darkenColoured(x2, y2, _thisTileOpacity, lightmap))//, distFromLightSrc))
}
else {
_thisTileOpacity2.set(_mapThisTileOpacity2.getVec(x, y))
_ambientAccumulator.maxAndAssign(darkenColoured(x2, y2, _thisTileOpacity2, lightmap, distFromLightSrc))
_mapThisTileOpacity2.getAndSet(_thisTileOpacity2, x, y)
_ambientAccumulator.maxAndAssign(darkenColoured(x2, y2, _thisTileOpacity2, lightmap))//, distFromLightSrc))
}
_mapLightLevelThis.setVec(x, y, _ambientAccumulator)
@@ -574,7 +576,7 @@ object LightmapRenderer {
// conduct the task #1
// spread towards the end
_swipeTask(swipeX, swipeY, swipeX-dx, swipeY-dy, lightmap, distFromLightSrc)
_swipeTask(swipeX, swipeY, swipeX-dx, swipeY-dy, lightmap)//, distFromLightSrc)
swipeX += dx
swipeY += dy
@@ -594,7 +596,7 @@ object LightmapRenderer {
while (swipeX*dx >= sx*dx && swipeY*dy >= sy*dy) {
// conduct the task #2
// spread towards the start
_swipeTask(swipeX, swipeY, swipeX+dx, swipeY+dy, lightmap, distFromLightSrc)
_swipeTask(swipeX, swipeY, swipeX+dx, swipeY+dy, lightmap)//, distFromLightSrc)
swipeX -= dx
swipeY -= dy
@@ -781,7 +783,7 @@ object LightmapRenderer {
* @param darken (0-255) per channel
* @return darkened data (0-255) per channel
*/
internal fun darkenColoured(x: Int, y: Int, darken: Cvec, lightmap: UnsafeCvecArray, distFromLightSrc: Ivec4 = Ivec4()): Cvec {
internal fun darkenColoured(x: Int, y: Int, darken: Cvec, lightmap: UnsafeCvecArray): Cvec {//, distFromLightSrc: Ivec4 = Ivec4()): Cvec {
// use equation with magic number 8.0
// this function, when done recursively (A_x = darken(A_x-1, C)), draws exponential curve. (R^2 = 1)