mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
unsafeptr is now byte-aligned
This commit is contained in:
@@ -16,7 +16,7 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
|
|||||||
private val array = UnsafeHelper.allocate(TOTAL_SIZE_IN_BYTES)
|
private val array = UnsafeHelper.allocate(TOTAL_SIZE_IN_BYTES)
|
||||||
val ptr = array.ptr
|
val ptr = array.ptr
|
||||||
|
|
||||||
private inline fun toAddr(x: Int, y: Int) = 16L * (y * width + x)
|
private inline fun toAddr(x: Int, y: Int) = 4L * (y * width + x)
|
||||||
|
|
||||||
fun isDestroyed() = array.destroyed
|
fun isDestroyed() = array.destroyed
|
||||||
|
|
||||||
@@ -26,43 +26,43 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
|
|||||||
|
|
||||||
// getters
|
// getters
|
||||||
fun getR(x: Int, y: Int) = array.getFloat(toAddr(x, y))
|
fun getR(x: Int, y: Int) = array.getFloat(toAddr(x, y))
|
||||||
fun getG(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 4)
|
fun getG(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 1)
|
||||||
fun getB(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 8)
|
fun getB(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 2)
|
||||||
fun getA(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 12)
|
fun getA(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 3)
|
||||||
inline fun getVec(x: Int, y: Int) = Cvec(
|
inline fun getVec(x: Int, y: Int) = Cvec(
|
||||||
array.getFloat(toAddr(x, y)),
|
array.getFloat(toAddr(x, y)),
|
||||||
array.getFloat(toAddr(x, y) + 4),
|
array.getFloat(toAddr(x, y) + 1),
|
||||||
array.getFloat(toAddr(x, y) + 8),
|
array.getFloat(toAddr(x, y) + 2),
|
||||||
array.getFloat(toAddr(x, y) + 12)
|
array.getFloat(toAddr(x, y) + 3)
|
||||||
)
|
)
|
||||||
/**
|
/**
|
||||||
* @param channel 0 for R, 1 for G, 2 for B, 3 for A
|
* @param channel 0 for R, 1 for G, 2 for B, 3 for A
|
||||||
*/
|
*/
|
||||||
fun channelGet(x: Int, y: Int, channel: Int) = array.getFloat(toAddr(x, y) + 4L * channel)
|
fun channelGet(x: Int, y: Int, channel: Int) = array.getFloat(toAddr(x, y) + channel)
|
||||||
|
|
||||||
// setters
|
// setters
|
||||||
fun zerofill() = array.fillWith(0)
|
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) + 4, 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) + 8, 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) + 12, value) }
|
fun setA(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y) + 3, value) }
|
||||||
inline fun setVec(x: Int, y: Int, value: Cvec) {
|
inline fun setVec(x: Int, y: Int, value: Cvec) {
|
||||||
array.setFloat(toAddr(x, y), value.r)
|
array.setFloat(toAddr(x, y), value.r)
|
||||||
array.setFloat(toAddr(x, y) + 4, value.g)
|
array.setFloat(toAddr(x, y) + 1, value.g)
|
||||||
array.setFloat(toAddr(x, y) + 8, value.b)
|
array.setFloat(toAddr(x, y) + 2, value.b)
|
||||||
array.setFloat(toAddr(x, y) + 12, value.a)
|
array.setFloat(toAddr(x, y) + 3, value.a)
|
||||||
}
|
}
|
||||||
inline fun setScalar(x: Int, y: Int, value: Float) {
|
inline fun setScalar(x: Int, y: Int, value: Float) {
|
||||||
array.setFloat(toAddr(x, y), value)
|
array.setFloat(toAddr(x, y), value)
|
||||||
array.setFloat(toAddr(x, y) + 4, value)
|
array.setFloat(toAddr(x, y) + 1, value)
|
||||||
array.setFloat(toAddr(x, y) + 8, value)
|
array.setFloat(toAddr(x, y) + 2, value)
|
||||||
array.setFloat(toAddr(x, y) + 12, value)
|
array.setFloat(toAddr(x, y) + 3, value)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param channel 0 for R, 1 for G, 2 for B, 3 for A
|
* @param channel 0 for R, 1 for G, 2 for B, 3 for A
|
||||||
*/
|
*/
|
||||||
fun channelSet(x: Int, y: Int, channel: Int, value: Float) {
|
fun channelSet(x: Int, y: Int, channel: Int, value: Float) {
|
||||||
array.setFloat(toAddr(x, y) + 4L * channel, value)
|
array.setFloat(toAddr(x, y) + channel, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// operators
|
// operators
|
||||||
@@ -82,20 +82,20 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
|
|||||||
fun mulAndAssign(x: Int, y: Int, scalar: Float) {
|
fun mulAndAssign(x: Int, y: Int, scalar: Float) {
|
||||||
val addr = toAddr(x, y)
|
val addr = toAddr(x, y)
|
||||||
for (k in 0..3) {
|
for (k in 0..3) {
|
||||||
array.setFloat(addr + 4*k, (array.getFloat(addr + 4*k) * scalar))
|
array.setFloat(addr + k, (array.getFloat(addr + k) * scalar))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun forAllMulAssign(scalar: Float) {
|
fun forAllMulAssign(scalar: Float) {
|
||||||
for (i in 0 until TOTAL_SIZE_IN_BYTES step 4) {
|
for (i in 0 until TOTAL_SIZE_IN_BYTES / 4) {
|
||||||
array.setFloat(i, array.getFloat(i) * scalar)
|
array.setFloat(i, array.getFloat(i) * scalar)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun forAllMulAssign(vector: Cvec) {
|
fun forAllMulAssign(vector: Cvec) {
|
||||||
for (i in 0 until TOTAL_SIZE_IN_BYTES step 16) {
|
for (i in 0 until TOTAL_SIZE_IN_BYTES / 4 step 4) {
|
||||||
for (k in 0 until 4) {
|
for (k in 0 until 4) {
|
||||||
array.setFloat(i + 4*k, array.getFloat(i + 4*k) * vector.getElem(k))
|
array.setFloat(i + 4*k, array.getFloat(i + k) * vector.getElem(k))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package net.torvald.terrarum.modulebasegame.gameactors
|
package net.torvald.terrarum.modulebasegame.gameactors
|
||||||
|
|
||||||
|
import net.torvald.terrarum.gameactors.ActorMovingPlatform
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by minjaesong on 2022-03-02.
|
* Created by minjaesong on 2022-03-02.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -82,11 +82,11 @@ internal object LightmapHDRMap : Disposable {
|
|||||||
|
|
||||||
operator fun invoke() {
|
operator fun invoke() {
|
||||||
THE_LUT.forEachIndexed { i, f ->
|
THE_LUT.forEachIndexed { i, f ->
|
||||||
ptr.setFloat(4L * i, f)
|
ptr.setFloat(i.toLong(), f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline operator fun get(index: Int) = ptr.getFloat(4L * index)
|
inline operator fun get(index: Int) = ptr.getFloat(index.toLong())
|
||||||
|
|
||||||
val lastIndex = THE_LUT.lastIndex
|
val lastIndex = THE_LUT.lastIndex
|
||||||
val size = lastIndex + 1
|
val size = lastIndex + 1
|
||||||
|
|||||||
@@ -124,68 +124,68 @@ internal class UnsafePtr(pointer: Long, allocSize: Long) {
|
|||||||
UnsafeHelper.unsafe.putByte(ptr + index, value)
|
UnsafeHelper.unsafe.putByte(ptr + index, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: get/set multibyte values are NOT BYTE-ALIGNED!
|
// NOTE: get/set multibyte values ARE BYTE-ALIGNED!
|
||||||
|
|
||||||
fun getDouble(index: Long): Double {
|
fun getDouble(index: Long): Double {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 8L)
|
||||||
return UnsafeHelper.unsafe.getDouble(ptr + index)
|
return UnsafeHelper.unsafe.getDouble(ptr + (index * 8L))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getLong(index: Long): Long {
|
fun getLong(index: Long): Long {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 8L)
|
||||||
return UnsafeHelper.unsafe.getLong(ptr + index)
|
return UnsafeHelper.unsafe.getLong(ptr + (index * 8L))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getFloat(index: Long): Float {
|
fun getFloat(index: Long): Float {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 4L)
|
||||||
return UnsafeHelper.unsafe.getFloat(ptr + index)
|
return UnsafeHelper.unsafe.getFloat(ptr + (index * 4L))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getInt(index: Long): Int {
|
fun getInt(index: Long): Int {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 4L)
|
||||||
return UnsafeHelper.unsafe.getInt(ptr + index)
|
return UnsafeHelper.unsafe.getInt(ptr + (index * 4L))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getShort(index: Long): Short {
|
fun getShort(index: Long): Short {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 2L)
|
||||||
return UnsafeHelper.unsafe.getShort(ptr + index)
|
return UnsafeHelper.unsafe.getShort(ptr + (index * 2L))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getChar(index: Long): Char {
|
fun getChar(index: Long): Char {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 2L)
|
||||||
return UnsafeHelper.unsafe.getChar(ptr + index)
|
return UnsafeHelper.unsafe.getChar(ptr + (index * 2L))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fun setDouble(index: Long, value: Double) {
|
fun setDouble(index: Long, value: Double) {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 8L)
|
||||||
UnsafeHelper.unsafe.putDouble(ptr + index, value)
|
UnsafeHelper.unsafe.putDouble(ptr + (index * 8L), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLong(index: Long, value: Long) {
|
fun setLong(index: Long, value: Long) {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 8L)
|
||||||
UnsafeHelper.unsafe.putLong(ptr + index, value)
|
UnsafeHelper.unsafe.putLong(ptr + (index * 8L), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setFloat(index: Long, value: Float) {
|
fun setFloat(index: Long, value: Float) {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 4L)
|
||||||
UnsafeHelper.unsafe.putFloat(ptr + index, value)
|
UnsafeHelper.unsafe.putFloat(ptr + (index * 4L), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setInt(index: Long, value: Int) {
|
fun setInt(index: Long, value: Int) {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 4L)
|
||||||
UnsafeHelper.unsafe.putInt(ptr + index, value)
|
UnsafeHelper.unsafe.putInt(ptr + (index * 4L), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setShort(index: Long, value: Short) {
|
fun setShort(index: Long, value: Short) {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 2L)
|
||||||
UnsafeHelper.unsafe.putShort(ptr + index, value)
|
UnsafeHelper.unsafe.putShort(ptr + (index * 2L), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setChar(index: Long, value: Char) {
|
fun setChar(index: Long, value: Char) {
|
||||||
checkNullPtr(index)
|
checkNullPtr(index * 2L)
|
||||||
UnsafeHelper.unsafe.putChar(ptr + index, value)
|
UnsafeHelper.unsafe.putChar(ptr + (index * 2L), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user