Skip to content

Commit c0d7733

Browse files
committed
Chunked memcpr and misc
1 parent 66c4b08 commit c0d7733

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

common/src/main/kotlin/com/lambda/graphics/buffer/vertex/VertexArray.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ class VertexArray(
4949
private fun renderInternal(
5050
indicesSize: Long,
5151
indicesPointer: Long,
52-
verticesOffset: Int
52+
verticesOffset: Long
5353
) = bind {
5454
glDrawElementsBaseVertex(
5555
vertexMode.mode,
56-
indicesSize.toInt() / UInt.SIZE_BYTES,
56+
indicesSize.toInt() / Int.SIZE_BYTES,
5757
GL_UNSIGNED_INT,
5858
indicesPointer,
59-
verticesOffset / attributes.stride
59+
verticesOffset.toInt() / attributes.stride,
6060
)
6161
}
6262

common/src/main/kotlin/com/lambda/graphics/pipeline/PersistentBuffer.kt

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import com.lambda.graphics.buffer.Buffer.Companion.createPipelineBuffer
2121
import com.lambda.graphics.buffer.DynamicByteBuffer
2222
import com.lambda.graphics.buffer.DynamicByteBuffer.Companion.dynamicByteBuffer
2323
import com.lambda.graphics.gl.kibibyte
24+
import org.lwjgl.system.MemoryUtil
2425
import 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
}

common/src/main/kotlin/com/lambda/graphics/pipeline/VertexPipeline.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class VertexPipeline(
4141
private val vao = VertexArray(vertexMode, attributes)
4242

4343
private val vbo = PersistentBuffer(GL_ARRAY_BUFFER, attributes.stride)
44-
private val ibo = PersistentBuffer(GL_ELEMENT_ARRAY_BUFFER, UInt.SIZE_BYTES)
44+
private val ibo = PersistentBuffer(GL_ELEMENT_ARRAY_BUFFER, Int.SIZE_BYTES)
4545

4646
init {
4747
vao.linkVbo(vbo)

common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/CachedString.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CachedString(
7676
/* Create gl buffers and upload */
7777
val vao = FontRenderer.vao
7878
vbo = PersistentBuffer(GL_ARRAY_BUFFER, vao.attributes.stride, builder.vertices.size)
79-
ibo = PersistentBuffer(GL_ELEMENT_ARRAY_BUFFER, UInt.SIZE_BYTES, builder.indices.size)
79+
ibo = PersistentBuffer(GL_ELEMENT_ARRAY_BUFFER, Int.SIZE_BYTES, builder.indices.size)
8080
vao.linkVbo(vbo)
8181

8282
builder.uploadVertices(vbo.byteBuffer)

0 commit comments

Comments
 (0)