@@ -21,8 +21,8 @@ import com.lambda.graphics.buffer.Buffer.Companion.createPipelineBuffer
2121import com.lambda.graphics.buffer.DynamicByteBuffer
2222import com.lambda.graphics.buffer.DynamicByteBuffer.Companion.dynamicByteBuffer
2323import com.lambda.graphics.gl.kibibyte
24+ import org.lwjgl.system.MemoryUtil
2425import org.lwjgl.system.MemoryUtil.memCopy
25- import sun.misc.Unsafe
2626
2727/* *
2828 * Represents a persistent dynamic coherent buffer for fast opengl rendering purposes
@@ -47,7 +47,7 @@ class PersistentBuffer(
4747 val glBuffer = createPipelineBuffer(target)
4848 private var glSize = 0
4949
50- var uploadOffset = 0
50+ var uploadOffset = 0L
5151
5252 fun upload () {
5353 val dataStart = byteBuffer.pointer + uploadOffset
@@ -64,14 +64,14 @@ class PersistentBuffer(
6464 }
6565
6666 if (snapshotData > 0 && snapshot.capacity >= byteBuffer.bytesPut) {
67- if (memcmp(snapshot, byteBuffer, uploadOffset, dataCount.toInt() )) return
67+ if (memcmp(snapshot, byteBuffer, uploadOffset, dataCount)) return
6868 }
6969
70- glBuffer.update(uploadOffset.toLong() , dataCount, dataStart)
70+ glBuffer.update(uploadOffset, dataCount, dataStart)
7171 }
7272
7373 fun end () {
74- uploadOffset = byteBuffer.bytesPut.toInt()
74+ uploadOffset = byteBuffer.bytesPut
7575 }
7676
7777 fun sync () {
@@ -91,21 +91,29 @@ class PersistentBuffer(
9191
9292 fun use (block : () -> Unit ) = glBuffer.bind { block() }
9393
94- private fun memcmp (a : DynamicByteBuffer , b : DynamicByteBuffer , position : Int , size : Int ): Boolean {
95- for (i in position.. < (position + size)) {
96- if (UNSAFE .getByte(null , a.pointer + i) !=
97- UNSAFE .getByte(null , b.pointer + i)) {
98- return false
99- }
94+ private fun memcmp (a : DynamicByteBuffer , b : DynamicByteBuffer , position : Long , size : Long ): Boolean {
95+ if (a.capacity != b.capacity) return false
96+
97+ val end = position + size
98+ var head = position
99+
100+ // Process the aligned bytes in chunks of 8 until we've reached the end
101+ while (head + 8 <= end) {
102+ val first = MemoryUtil .memGetLong(a.pointer + head)
103+ val second = MemoryUtil .memGetLong(b.pointer + head)
104+ if (first != second) return false
105+
106+ head + = 8
100107 }
101- return true
102- }
103108
104- companion object {
105- private val UNSAFE = run {
106- val unsafeField = Unsafe ::class .java.getDeclaredField(" theUnsafe" )
107- unsafeField.setAccessible(true )
108- unsafeField.get(null ) as Unsafe
109+ while (head < end) {
110+ val first = MemoryUtil .memGetByte(a.pointer + head)
111+ val second = MemoryUtil .memGetByte(b.pointer + head)
112+ if (first != second) return false
113+
114+ head++
109115 }
116+
117+ return true
110118 }
111119}
0 commit comments