unsafeptr is now byte-aligned

This commit is contained in:
minjaesong
2022-04-06 20:54:58 +09:00
parent d958683365
commit 97633eba46
4 changed files with 51 additions and 49 deletions

View File

@@ -16,7 +16,7 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
private val array = UnsafeHelper.allocate(TOTAL_SIZE_IN_BYTES)
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
@@ -26,43 +26,43 @@ 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) + 4)
fun getB(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 8)
fun getA(x: Int, y: Int) = array.getFloat(toAddr(x, y) + 12)
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)
inline fun getVec(x: Int, y: Int) = Cvec(
array.getFloat(toAddr(x, y)),
array.getFloat(toAddr(x, y) + 4),
array.getFloat(toAddr(x, y) + 8),
array.getFloat(toAddr(x, y) + 12)
array.getFloat(toAddr(x, y) + 1),
array.getFloat(toAddr(x, y) + 2),
array.getFloat(toAddr(x, y) + 3)
)
/**
* @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
fun zerofill() = array.fillWith(0)
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 setB(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y) + 8, value) }
fun setA(x: Int, y: Int, value: Float) { array.setFloat(toAddr(x, y) + 12, 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) }
inline fun setVec(x: Int, y: Int, value: Cvec) {
array.setFloat(toAddr(x, y), value.r)
array.setFloat(toAddr(x, y) + 4, value.g)
array.setFloat(toAddr(x, y) + 8, value.b)
array.setFloat(toAddr(x, y) + 12, value.a)
array.setFloat(toAddr(x, y) + 1, value.g)
array.setFloat(toAddr(x, y) + 2, value.b)
array.setFloat(toAddr(x, y) + 3, value.a)
}
inline fun setScalar(x: Int, y: Int, value: Float) {
array.setFloat(toAddr(x, y), value)
array.setFloat(toAddr(x, y) + 4, value)
array.setFloat(toAddr(x, y) + 8, value)
array.setFloat(toAddr(x, y) + 12, value)
array.setFloat(toAddr(x, y) + 1, value)
array.setFloat(toAddr(x, y) + 2, value)
array.setFloat(toAddr(x, y) + 3, value)
}
/**
* @param channel 0 for R, 1 for G, 2 for B, 3 for A
*/
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
@@ -82,20 +82,20 @@ internal class UnsafeCvecArray(val width: Int, val height: Int) {
fun mulAndAssign(x: Int, y: Int, scalar: Float) {
val addr = toAddr(x, y)
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) {
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)
}
}
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) {
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))
}
}
}

View File

@@ -1,5 +1,7 @@
package net.torvald.terrarum.modulebasegame.gameactors
import net.torvald.terrarum.gameactors.ActorMovingPlatform
/**
* Created by minjaesong on 2022-03-02.
*/

View File

@@ -82,11 +82,11 @@ internal object LightmapHDRMap : Disposable {
operator fun invoke() {
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 size = lastIndex + 1

View File

@@ -124,68 +124,68 @@ internal class UnsafePtr(pointer: Long, allocSize: Long) {
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 {
checkNullPtr(index)
return UnsafeHelper.unsafe.getDouble(ptr + index)
checkNullPtr(index * 8L)
return UnsafeHelper.unsafe.getDouble(ptr + (index * 8L))
}
fun getLong(index: Long): Long {
checkNullPtr(index)
return UnsafeHelper.unsafe.getLong(ptr + index)
checkNullPtr(index * 8L)
return UnsafeHelper.unsafe.getLong(ptr + (index * 8L))
}
fun getFloat(index: Long): Float {
checkNullPtr(index)
return UnsafeHelper.unsafe.getFloat(ptr + index)
checkNullPtr(index * 4L)
return UnsafeHelper.unsafe.getFloat(ptr + (index * 4L))
}
fun getInt(index: Long): Int {
checkNullPtr(index)
return UnsafeHelper.unsafe.getInt(ptr + index)
checkNullPtr(index * 4L)
return UnsafeHelper.unsafe.getInt(ptr + (index * 4L))
}
fun getShort(index: Long): Short {
checkNullPtr(index)
return UnsafeHelper.unsafe.getShort(ptr + index)
checkNullPtr(index * 2L)
return UnsafeHelper.unsafe.getShort(ptr + (index * 2L))
}
fun getChar(index: Long): Char {
checkNullPtr(index)
return UnsafeHelper.unsafe.getChar(ptr + index)
checkNullPtr(index * 2L)
return UnsafeHelper.unsafe.getChar(ptr + (index * 2L))
}
fun setDouble(index: Long, value: Double) {
checkNullPtr(index)
UnsafeHelper.unsafe.putDouble(ptr + index, value)
checkNullPtr(index * 8L)
UnsafeHelper.unsafe.putDouble(ptr + (index * 8L), value)
}
fun setLong(index: Long, value: Long) {
checkNullPtr(index)
UnsafeHelper.unsafe.putLong(ptr + index, value)
checkNullPtr(index * 8L)
UnsafeHelper.unsafe.putLong(ptr + (index * 8L), value)
}
fun setFloat(index: Long, value: Float) {
checkNullPtr(index)
UnsafeHelper.unsafe.putFloat(ptr + index, value)
checkNullPtr(index * 4L)
UnsafeHelper.unsafe.putFloat(ptr + (index * 4L), value)
}
fun setInt(index: Long, value: Int) {
checkNullPtr(index)
UnsafeHelper.unsafe.putInt(ptr + index, value)
checkNullPtr(index * 4L)
UnsafeHelper.unsafe.putInt(ptr + (index * 4L), value)
}
fun setShort(index: Long, value: Short) {
checkNullPtr(index)
UnsafeHelper.unsafe.putShort(ptr + index, value)
checkNullPtr(index * 2L)
UnsafeHelper.unsafe.putShort(ptr + (index * 2L), value)
}
fun setChar(index: Long, value: Char) {
checkNullPtr(index)
UnsafeHelper.unsafe.putChar(ptr + index, value)
checkNullPtr(index * 2L)
UnsafeHelper.unsafe.putChar(ptr + (index * 2L), value)
}