made it work but slower?!

This commit is contained in:
minjaesong
2019-05-21 17:48:10 +09:00
parent e3fbb57530
commit 046f2d4b6c
13 changed files with 118 additions and 127 deletions

2
.idea/misc.xml generated
View File

@@ -38,7 +38,7 @@
<property name="caretWidth" class="java.lang.Integer" />
</properties>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="13 (Panama)" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="false" project-jdk-name="13-incubate" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -19,9 +19,10 @@ class GdxColorMap {
height = pixmap.height
is2D = pixmap.height > 1
data = kotlin.IntArray(pixmap.width * pixmap.height) {
dataRaw = kotlin.IntArray(pixmap.width * pixmap.height) {
pixmap.getPixel(it % pixmap.width, it / pixmap.width)
}
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
pixmap.dispose()
}
@@ -31,39 +32,46 @@ class GdxColorMap {
height = pixmap.height
is2D = pixmap.height > 1
data = kotlin.IntArray(pixmap.width * pixmap.height) {
dataRaw = kotlin.IntArray(pixmap.width * pixmap.height) {
pixmap.getPixel(it % pixmap.width, it / pixmap.width)
}
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
if (disposePixmap) pixmap.dispose()
}
constructor(color: Color) {
data = intArrayOf(color.toIntBits())
dataRaw = intArrayOf(color.toIntBits())
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
width = 1
height = 1
is2D = false
}
constructor(gradStart: Color, gradEnd: Color) {
data = intArrayOf(gradStart.toIntBits(), gradEnd.toIntBits())
dataRaw = intArrayOf(gradStart.toIntBits(), gradEnd.toIntBits())
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
width = 1
height = 2
is2D = true
}
private val data: IntArray
private val dataRaw: IntArray
private val dataGdxColor: Array<Color>
//private val dataCvec: Array<Cvec>
val width: Int
val height: Int
val is2D: Boolean
fun get(x: Int, y: Int): Color = Color(data[y * width + x])
operator fun get(x: Int): Color = if (is2D) throw OperationNotSupportedException("This is 2D color map") else Color(data[x])
fun get(x: Int, y: Int): Color = dataGdxColor[y * width + x]
operator fun get(x: Int): Color = if (is2D) throw OperationNotSupportedException("This is 2D color map") else dataGdxColor[x]
fun getRaw(x: Int, y: Int): RGBA8888 = data[y * width + x]
fun getRaw(x: Int): RGBA8888 = if (is2D) throw OperationNotSupportedException("This is 2D color map") else data[x]
fun getRaw(x: Int, y: Int): RGBA8888 = dataRaw[y * width + x]
fun getRaw(x: Int): RGBA8888 = if (is2D) throw OperationNotSupportedException("This is 2D color map") else dataRaw[x]
//fun getAsCvec(x: Int, y: Int): Cvec = dataCvec[y * width + x]
override fun toString(): String {
val sb = StringBuilder()

View File

@@ -110,7 +110,7 @@ object BlockCodex {
prop.shadeColG = floatVal(record, "shdg") / LightmapRenderer.MUL_FLOAT
prop.shadeColB = floatVal(record, "shdb") / LightmapRenderer.MUL_FLOAT
prop.shadeColA = floatVal(record, "shduv") / LightmapRenderer.MUL_FLOAT
prop.opacity = Cvec(floatArrayOf(prop.shadeColR, prop.shadeColG, prop.shadeColB, prop.shadeColA))
prop.opacity = Cvec(prop.shadeColR, prop.shadeColG, prop.shadeColB, prop.shadeColA)
prop.strength = intVal(record, "str")
prop.density = intVal(record, "dsty")
@@ -119,7 +119,7 @@ object BlockCodex {
prop.lumColG = floatVal(record, "lumg") / LightmapRenderer.MUL_FLOAT
prop.lumColB = floatVal(record, "lumb") / LightmapRenderer.MUL_FLOAT
prop.lumColA = floatVal(record, "lumuv") / LightmapRenderer.MUL_FLOAT
prop.internalLumCol = Cvec(floatArrayOf(prop.lumColR, prop.lumColG, prop.lumColB, prop.lumColA))
prop.internalLumCol = Cvec(prop.lumColR, prop.lumColG, prop.lumColB, prop.lumColA)
prop.friction = intVal(record, "fr")
prop.viscosity = intVal(record, "vscs")

View File

@@ -17,7 +17,7 @@ class BlockProp {
var shadeColB = 0f
var shadeColA = 0f
lateinit var opacity: Cvec
var opacity: Cvec = Cvec()
var strength: Int = 0
var density: Int = 0
@@ -36,7 +36,7 @@ class BlockProp {
var lumColG = 0f
var lumColB = 0f
var lumColA = 0f
lateinit var internalLumCol: Cvec
var internalLumCol: Cvec = Cvec()
/**
* @param luminosity

View File

@@ -94,8 +94,8 @@ object BlockPropUtil {
fun getDynamicLumFunc(baseLum: Cvec, type: Int): Cvec {
return when (type) {
1 -> getTorchFlicker(baseLum)
2 -> (Terrarum.ingame!!.world).globalLight.cpy().mul(LightmapRenderer.DIV_FLOAT) // current global light
3 -> WeatherMixer.getGlobalLightOfTime(WorldTime.DAY_LENGTH / 2).cpy().mul(LightmapRenderer.DIV_FLOAT) // daylight at noon
2 -> (Terrarum.ingame!!.world).globalLight * LightmapRenderer.DIV_FLOAT // current global light
3 -> WeatherMixer.getGlobalLightOfTime(WorldTime.DAY_LENGTH / 2) * LightmapRenderer.DIV_FLOAT // daylight at noon
4 -> getSlowBreath(baseLum)
5 -> getPulsate(baseLum)
else -> baseLum

View File

@@ -13,11 +13,12 @@ internal object SetGlobalLightOverride : ConsoleCommand {
override fun execute(args: Array<String>) {
if (args.size == 5) {
try {
//val r = args[1].toFloat()
//val g = args[2].toFloat()
//val b = args[3].toFloat()
//val a = args[4].toFloat()
val GL = Cvec(args.sliceArray(1..4).map { it.toFloat() }.toFloatArray())
val GL = Cvec(
args[1].toFloat(),
args[2].toFloat(),
args[3].toFloat(),
args[4].toFloat()
)
WeatherMixer.globalLightOverridden = true
(Terrarum.ingame!!.world).globalLight = GL

View File

@@ -69,17 +69,17 @@ open class ActorHumanoid(
}
override var color: Cvec
get() = Cvec(floatArrayOf(
get() = Cvec(
(actorValue.getAsFloat(AVKey.LUMR) ?: 0f) / LightmapRenderer.MUL_FLOAT,
(actorValue.getAsFloat(AVKey.LUMG) ?: 0f) / LightmapRenderer.MUL_FLOAT,
(actorValue.getAsFloat(AVKey.LUMB) ?: 0f) / LightmapRenderer.MUL_FLOAT,
(actorValue.getAsFloat(AVKey.LUMA) ?: 0f) / LightmapRenderer.MUL_FLOAT
))
)
set(value) {
actorValue[AVKey.LUMR] = value.vec[0] * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMG] = value.vec[1] * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMB] = value.vec[2] * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMA] = value.vec[3] * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMR] = value.vec.lane(0) * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMG] = value.vec.lane(1) * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMB] = value.vec.lane(2) * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMA] = value.vec.lane(3) * LightmapRenderer.MUL_FLOAT
}
/**

View File

@@ -33,7 +33,7 @@ open class ProjectileSimple(
override var color: Cvec
get() = (bulletDatabase[type][OFFSET_LUMINOSITY] as Cvec).cpy()
get() = (bulletDatabase[type][OFFSET_LUMINOSITY] as Cvec)
set(value) {
}
/**

View File

@@ -52,7 +52,8 @@ internal object WeatherMixer : RNGConsumer {
lateinit var mixedWeather: BaseModularWeather
val globalLightNow = Cvec()
var globalLightNow = Cvec()
private set
// Weather indices
const val WEATHER_GENERIC = "generic"
@@ -134,7 +135,7 @@ internal object WeatherMixer : RNGConsumer {
// calculate global light
val globalLight = getGradientColour(skyboxColourMap, 2, timeNow)
globalLightNow.setTo(floatArrayOf(globalLight.r, globalLight.g, globalLight.b, globalLight.a))
globalLightNow = Cvec(globalLight.r, globalLight.g, globalLight.b, globalLight.a)
/* (copied from the shader source)
@@ -181,7 +182,7 @@ internal object WeatherMixer : RNGConsumer {
*/
fun getGlobalLightOfTime(timeInSec: Int): Cvec {
val c = getGradientColour(currentWeather.skyboxGradColourMap, 2, timeInSec)
return Cvec(floatArrayOf(c.r, c.g, c.b, c.a))
return Cvec(c.r, c.g, c.b, c.a)
}
fun getGradientColour(colorMap: GdxColorMap, row: Int, timeInSec: Int): Color {

View File

@@ -140,10 +140,10 @@ class BasicDebugInfoWindow : UICanvas() {
val mtX = mouseTileX.toString()
val mtY = mouseTileY.toString()
val valRaw = LightmapRenderer.getLight(mouseTileX, mouseTileY)
val rawR = valRaw?.vec?.get(0)?.times(100f)?.round()?.div(100f)
val rawG = valRaw?.vec?.get(1)?.times(100f)?.round()?.div(100f)
val rawB = valRaw?.vec?.get(2)?.times(100f)?.round()?.div(100f)
val rawA = valRaw?.vec?.get(3)?.times(100f)?.round()?.div(100f)
val rawR = valRaw?.vec?.lane(0)?.times(100f)?.round()?.div(100f)
val rawG = valRaw?.vec?.lane(1)?.times(100f)?.round()?.div(100f)
val rawB = valRaw?.vec?.lane(2)?.times(100f)?.round()?.div(100f)
val rawA = valRaw?.vec?.lane(3)?.times(100f)?.round()?.div(100f)
lightVal = if (valRaw == null) ""
else "$rawR $rawG $rawB $rawA"

View File

@@ -1,75 +1,64 @@
package net.torvald.terrarum.worlddrawer
import jdk.incubator.vector.FloatVector
import kotlin.math.roundToInt
//import jdk.incubator.vector.FloatVector
/**
* Get your panama JDK for linux/mac/windows at https://jdk.java.net/panama/
* Get and compile your OpenJDK-Panama for linux/mac/windows at https://jdk.java.net/panama/
* Or use pre-built ones in https://github.com/minjaesong/openjdk13-vectorintrinsic/tree/master
*
* NOTE: Panama's new vectors are all immutable.
* NOTE2: Class inlining vastly improves the performance
*
* -XX:TypeProfileLevel=121 -XX:+UseVectorApiIntrinsics --add-modules=jdk.incubator.vector
*
* Created by minjaesong on 2019-05-20.
*/
class Cvec(val vec: FloatArray) {
inline class Cvec constructor(val vec: FloatVector) {
constructor(r: Float, g: Float, b: Float, a: Float) : this(floatArrayOf(r, g, b, a))
constructor(scalar: Float) : this(FloatArray(4) { scalar })
constructor() : this(FloatArray(4) { 0f })
//constructor(floatArray: FloatArray) : this(FloatVector.fromArray(SPECIES, floatArray, 0))
constructor(r: Float, g: Float, b: Float, a: Float) : this(FloatVector.scalars(SPECIES, r, g, b, a))
constructor(scalar: Float) : this(FloatVector.scalars(SPECIES, scalar, scalar, scalar, scalar))
constructor() : this(FloatVector.zero(SPECIES))
private val epsilon = 1f / 512f
fun cpy(): Cvec = Cvec(this.vec)
fun setTo(scalar: Float) = setTo(FloatArray(4) { scalar })
fun setTo(other: Cvec) = setTo(other.vec)
fun setTo(other: FloatArray): Cvec {
for (i in 0..3) {
this.vec[i] = other[i]
}
return this
companion object {
private val EPSILON = 1f / 512f
private val SPECIES = FloatVector.SPECIES_256
}
infix fun mul(scalar: Float): Cvec = mul(FloatArray(4) { scalar })
infix fun mul(other: Cvec) = mul(other.vec)
//fun cpy(): Cvec = Cvec(this.vec)
fun mul(other: FloatArray): Cvec {
for (i in 0..3) {
this.vec[i] *= other[i]
}
return this
}
fun multiply(other: FloatVector) = Cvec(vec.mul(other))
infix operator fun times(other: Cvec) = multiply(other.vec)
infix operator fun times(scalar: Float) = Cvec(vec.mul(scalar))
fun max(other: Cvec): Cvec = max(other.vec)
fun subtract(other: FloatVector) = Cvec(vec.sub(other))
infix operator fun minus(other: Cvec) = subtract(other.vec)
infix operator fun minus(scalar: Float) = Cvec(vec.sub(scalar))
fun max(other: FloatArray): Cvec {
for (i in 0..3) {
this.vec[i] = if (this.vec[i] >= other[i]) this.vec[i] else other[i]
}
return this
}
fun addition(other: FloatVector) = Cvec(vec.add(other))
infix operator fun plus(other: Cvec) = addition(other.vec)
infix operator fun plus(scalar: Float) = Cvec(vec.add(scalar))
fun maximum(other: FloatVector): Cvec = Cvec(vec.max(other))
infix fun max(other: Cvec): Cvec = maximum(other.vec)
/**
* true if at least one element in the vector is not zero.
*/
fun nonZero(): Boolean {
var oracc = 0 // set to 1 if the vector element is zero
for (i in 0..3) {
if (vec[i] in 0f..epsilon)
oracc = oracc or 1
}
return (oracc != 0)
}
fun nonZero(): Boolean = vec.mulLanes() != 0f
fun toRGBA8888(): Int {
var acc = 0
for (i in 0..3)
acc += vec[i].coerceIn(0f, 1f).times(255f).roundToInt().shl(8 * (3 - i))
acc += vec.lane(i).coerceIn(0f, 1f).times(255f).roundToInt().shl(8 * (3 - i))
return acc
}
/*override fun equals(other: Any?): Boolean {
return this.vec.equal((other as Cvec).vec).allTrue()
}*/
}
//hg clone http://hg.openjdk.java.net/panama/dev/

View File

@@ -126,7 +126,7 @@ object LightmapRenderer {
return null
}
else {
return col.cpy().mul(MUL_FLOAT)
return col * MUL_FLOAT
}
}
@@ -220,8 +220,7 @@ object LightmapRenderer {
*/
// set sunlight
sunLight.setTo(world.globalLight)
sunLight.mul(DIV_FLOAT)
sunLight = world.globalLight * DIV_FLOAT
// set no-op mask from solidity of the block
AppLoader.measureDebugTime("Renderer.LightNoOpMask") {
@@ -395,15 +394,15 @@ object LightmapRenderer {
//private val ambientAccumulator = Color(0f,0f,0f,0f)
private val lightLevelThis = Cvec()
private var lightLevelThis = Cvec()
private var thisTerrain = 0
private var thisFluid = GameWorld.FluidInfo(Fluid.NULL, 0f)
private val fluidAmountToCol = Cvec()
private var fluidAmountToCol = Cvec()
private var thisWall = 0
private val thisTileLuminosity = Cvec()
private val thisTileOpacity = Cvec()
private val thisTileOpacity2 = Cvec() // thisTileOpacity * sqrt(2)
private val sunLight = Cvec()
private var thisTileLuminosity = Cvec()
private var thisTileOpacity = Cvec()
private var thisTileOpacity2 = Cvec() // thisTileOpacity * sqrt(2)
private var sunLight = Cvec()
/**
* This function will alter following variables:
@@ -417,31 +416,31 @@ object LightmapRenderer {
* - sunlight
*/
private fun getLightsAndShades(x: Int, y: Int) {
lightLevelThis.setTo(colourNull)
lightLevelThis = colourNull
thisTerrain = world.getTileFromTerrain(x, y) ?: Block.STONE
thisFluid = world.getFluid(x, y)
thisWall = world.getTileFromWall(x, y) ?: Block.STONE
if (thisFluid.type != Fluid.NULL) {
fluidAmountToCol.setTo(thisFluid.amount)
fluidAmountToCol = Cvec(thisFluid.amount)
thisTileLuminosity.setTo(BlockCodex[thisTerrain].luminosity)
thisTileLuminosity.max(BlockCodex[thisFluid.type].luminosity mul fluidAmountToCol) // already been div by four
thisTileOpacity.setTo(BlockCodex[thisTerrain].opacity)
thisTileOpacity.max(BlockCodex[thisFluid.type].opacity mul fluidAmountToCol) // already been div by four
thisTileLuminosity = BlockCodex[thisTerrain].luminosity
thisTileLuminosity.max(BlockCodex[thisFluid.type].luminosity * fluidAmountToCol) // already been div by four
thisTileOpacity = BlockCodex[thisTerrain].opacity
thisTileOpacity.max(BlockCodex[thisFluid.type].opacity * fluidAmountToCol) // already been div by four
}
else {
thisTileLuminosity.setTo(BlockCodex[thisTerrain].luminosity)
thisTileOpacity.setTo(BlockCodex[thisTerrain].opacity)
thisTileLuminosity = BlockCodex[thisTerrain].luminosity
thisTileOpacity = BlockCodex[thisTerrain].opacity
}
thisTileOpacity2.setTo(thisTileOpacity); thisTileOpacity2.mul(1.41421356f)
thisTileOpacity2 = thisTileOpacity * 1.41421356f
//sunLight.set(world.globalLight); sunLight.mul(DIV_FLOAT) // moved to fireRecalculateEvent()
// open air || luminous tile backed by sunlight
if ((thisTerrain == AIR && thisWall == AIR) || (thisTileLuminosity.nonZero() && thisWall == AIR)) {
lightLevelThis.setTo(sunLight)
lightLevelThis = sunLight
}
// blend lantern
@@ -529,7 +528,7 @@ object LightmapRenderer {
//return lightLevelThis.cpy() // it HAS to be a cpy(), otherwise all cells gets the same instance
setLightOf(lightmap, x, y, lightLevelThis.cpy())
setLightOf(lightmap, x, y, lightLevelThis)
}
private fun getLightForOpaque(x: Int, y: Int): Cvec? { // ...so that they wouldn't appear too dark
@@ -538,7 +537,7 @@ object LightmapRenderer {
// brighten if solid
if (BlockCodex[world.getTileFromTerrain(x, y)].isSolid) {
return l.cpy().mul(1.2f)
return l * 1.2f
}
else {
return l
@@ -605,6 +604,7 @@ object LightmapRenderer {
}
private val CVEC_ONE = Cvec(1f)
val lightScalingMagic = 8f
/**
@@ -618,13 +618,9 @@ object LightmapRenderer {
// 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)
val newvec = data.cpy()
// equation: data * (1 - darken * magic)
for (i in 0..3) {
newvec.vec[i] = data.vec[i] * (1f - darken.vec[i] * lightScalingMagic)
}
return newvec
return data * (CVEC_ONE - darken * lightScalingMagic)
}
/**
@@ -650,13 +646,9 @@ object LightmapRenderer {
* @return processed colour
*/
fun alterBrightnessUniform(data: Cvec, brighten: Float): Cvec {
val newvec = data.cpy()
// equation = data + brighten
for (i in 0..3) {
newvec.vec[i] = data.vec[i] + brighten
}
return newvec
return data + brighten
}
@@ -784,13 +776,13 @@ object LightmapRenderer {
)
/** To eliminated visible edge on the gradient when 255/1023 is exceeded */
internal fun Cvec.normaliseToHDR(): Cvec {
val newvec = this.cpy()
for (i in 0..3) {
newvec.vec[i] = hdr(newvec.vec[i].coerceIn(0f,1f))
}
return newvec
// equation: hdr(this.coerceIn)
return Cvec(
hdr(this.vec.lane(0).coerceIn(0f, 1f)),
hdr(this.vec.lane(1).coerceIn(0f, 1f)),
hdr(this.vec.lane(2).coerceIn(0f, 1f)),
hdr(this.vec.lane(3).coerceIn(0f, 1f))
)
}
val histogram: Histogram
@@ -807,10 +799,10 @@ object LightmapRenderer {
try {
//val colour = lightmap[y][x]
val colour = lightmap[y * LIGHTMAP_WIDTH + x]
reds[minOf(CHANNEL_MAX, colour.vec[0].times(MUL).floorInt())] += 1
greens[minOf(CHANNEL_MAX, colour.vec[1].times(MUL).floorInt())] += 1
blues[minOf(CHANNEL_MAX, colour.vec[2].times(MUL).floorInt())] += 1
uvs[minOf(CHANNEL_MAX, colour.vec[3].times(MUL).floorInt())] += 1
reds[minOf(CHANNEL_MAX, colour.vec.lane(0).times(MUL).floorInt())] += 1
greens[minOf(CHANNEL_MAX, colour.vec.lane(1).times(MUL).floorInt())] += 1
blues[minOf(CHANNEL_MAX, colour.vec.lane(2).times(MUL).floorInt())] += 1
uvs[minOf(CHANNEL_MAX, colour.vec.lane(3).times(MUL).floorInt())] += 1
}
catch (e: ArrayIndexOutOfBoundsException) { }
}

View File

@@ -2,10 +2,10 @@
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="kotlin-language" name="Kotlin">
<configuration version="3" platform="JVM 1.8">
<configuration version="3" platform="JVM 9">
<compilerSettings />
<compilerArguments>
<option name="jvmTarget" value="1.8" />
<option name="jvmTarget" value="10" />
</compilerArguments>
</configuration>
</facet>
@@ -16,7 +16,7 @@
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/net/torvald/terrarum/tests" isTestSource="true" />
</content>
<orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="lib" level="project" />