This repository was archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cae39cb
commit 9ec0882
Showing
49 changed files
with
295 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
kgl-core/src/commonMain/kotlin/com/kgl/core/DirectMemory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.kgl.core | ||
|
||
expect class DirectMemory { | ||
val size: Long | ||
|
||
operator fun get(offset: Long): Byte | ||
operator fun set(offset: Long, value: Byte) | ||
|
||
fun get(dst: ByteArray, srcOffset: Long = 0L, dstOffset: Long = 0L, length: Long = dst.size.toLong()) | ||
fun set(src: ByteArray, srcOffset: Long = 0L, dstOffset: Long = 0L, length: Long = src.size.toLong()) | ||
|
||
fun copyTo(dst: DirectMemory, srcOffset: Long = 0L, dstOffset: Long = 0L, length: Long = dst.size - dstOffset) | ||
fun slice(offset: Long, length: Long): DirectMemory | ||
} | ||
|
||
@Suppress("NOTHING_TO_INLINE") | ||
internal inline fun DirectMemory.checkBounds(offset: Long, length: Long) { | ||
require(offset in 0 until this.size) | ||
require(length in 0 until (this.size - offset)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.kgl.core | ||
|
||
|
||
expect inline fun <T> withMemory(length: Long, block: (DirectMemory) -> T): T |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.kgl.core | ||
|
||
import org.khronos.webgl.DataView | ||
|
||
actual class DirectMemory(private val ptr: DataView) { | ||
actual val size: Long get() = ptr.byteLength.toLong() | ||
|
||
actual operator fun get(offset: Long): Byte { | ||
return ptr.getInt8(offset.toInt()) | ||
} | ||
|
||
actual operator fun set(offset: Long, value: Byte) { | ||
ptr.setInt8(offset.toInt(), value) | ||
} | ||
|
||
actual fun get(dst: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) { | ||
checkBounds(srcOffset, length) | ||
for (i in 0 until length) { | ||
dst[(dstOffset + i).toInt()] = get(srcOffset + i) | ||
} | ||
} | ||
|
||
actual fun set(src: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) { | ||
checkBounds(dstOffset, length) | ||
for (i in 0 until length) { | ||
this[dstOffset + i] = src[(srcOffset + i).toInt()] | ||
} | ||
} | ||
|
||
actual fun copyTo(dst: DirectMemory, srcOffset: Long, dstOffset: Long, length: Long) { | ||
checkBounds(srcOffset, length) | ||
dst.checkBounds(dstOffset, length) | ||
for (i in 0 until length) { | ||
dst[dstOffset + i] = get(srcOffset + i) | ||
} | ||
} | ||
|
||
actual fun slice(offset: Long, length: Long): DirectMemory { | ||
checkBounds(offset, length) | ||
return DirectMemory(DataView(ptr.buffer, ptr.byteOffset + offset.toInt(), length.toInt())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.kgl.core | ||
|
||
import org.khronos.webgl.DataView | ||
import org.khronos.webgl.Int8Array | ||
|
||
actual inline fun <T> withMemory(length: Long, block: (DirectMemory) -> T): T { | ||
val array = ByteArray(length.toInt()) | ||
val jsArray = array.unsafeCast<Int8Array>() | ||
val view = DataView(jsArray.buffer, jsArray.byteOffset, jsArray.byteLength) | ||
return block(DirectMemory(view)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.kgl.core | ||
|
||
import org.lwjgl.system.MemoryUtil | ||
import org.lwjgl.system.jni.JNINativeInterface | ||
import java.nio.ByteBuffer as JvmByteBuffer | ||
|
||
actual class DirectMemory(private val ptr: Long, actual val size: Long) { | ||
|
||
constructor(buffer: JvmByteBuffer) : this(MemoryUtil.memAddress(buffer), buffer.capacity().toLong()) | ||
|
||
actual operator fun get(offset: Long): Byte { | ||
return MemoryUtil.memGetByte(ptr + (offset * Byte.SIZE_BYTES)) | ||
} | ||
|
||
actual operator fun set(offset: Long, value: Byte) { | ||
MemoryUtil.memPutByte(ptr + (offset * Byte.SIZE_BYTES), value) | ||
} | ||
|
||
actual fun get(dst: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) { | ||
checkBounds(srcOffset, length) | ||
|
||
JNINativeInterface.nSetByteArrayRegion(dst, dstOffset.toInt(), length.toInt(), ptr + (srcOffset * Byte.SIZE_BYTES)) | ||
} | ||
|
||
actual fun set(src: ByteArray, srcOffset: Long, dstOffset: Long, length: Long) { | ||
checkBounds(dstOffset, length) | ||
JNINativeInterface.nGetByteArrayRegion(src, srcOffset.toInt(), length.toInt(), ptr + (dstOffset * Byte.SIZE_BYTES)) | ||
} | ||
|
||
actual fun copyTo(dst: DirectMemory, srcOffset: Long, dstOffset: Long, length: Long) { | ||
checkBounds(srcOffset, length) | ||
dst.checkBounds(dstOffset, length) | ||
|
||
MemoryUtil.memCopy( | ||
ptr + (srcOffset * Byte.SIZE_BYTES), | ||
dst.ptr + (dstOffset * Byte.SIZE_BYTES), | ||
length | ||
) | ||
} | ||
|
||
actual fun slice(offset: Long, length: Long): DirectMemory { | ||
checkBounds(offset, length) | ||
|
||
return DirectMemory(ptr + (offset * Byte.SIZE_BYTES), length) | ||
} | ||
|
||
fun asJvmByteBuffer(): JvmByteBuffer { | ||
return MemoryUtil.memByteBuffer(ptr, size.toInt()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.