Skip to content

Commit 2b224f3

Browse files
authored
Reduce buffer allocations (#2320)
* Clone data only if different size * Do not mark as dirty if data is the same
1 parent 3dbd36a commit 2b224f3

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

korge-core/src/korlibs/graphics/AGObjects.kt

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,39 @@ class AGBuffer : AGObject() {
4545
// @TODO: Allow upload range in addition to the full buffer.
4646
// @TODO: This will allow to upload chunks of uniform buffers for example.
4747
// glBufferData & glBufferSubData
48-
fun upload(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Int8Buffer(data, offset, length).buffer)
49-
fun upload(data: UByteArray): AGBuffer = upload(Uint8Buffer(data).buffer)
50-
fun upload(data: FloatArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Float32Buffer(data, offset, length).buffer)
51-
fun upload(data: IntArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Int32Buffer(data, offset, length).buffer)
52-
fun upload(data: ShortArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Int16Buffer(data, offset, length).buffer)
53-
fun upload(data: Buffer, offset: Int, length: Int = data.size - offset): AGBuffer = upload(data.sliceWithSize(offset, length))
54-
fun upload(data: Buffer): AGBuffer {
55-
//println(data.sizeInBytes)
56-
// Only check small buffers
57-
if (data.sizeInBytes < 1024) {
58-
if (this.mem != null && this.mem!!.sizeInBytes == data.sizeInBytes && arrayequal(this.mem!!, 0, data, 0, data.sizeInBytes)) return this
48+
fun upload(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
49+
upload(Int8Buffer(data, offset, length).buffer, needClone = false)
50+
51+
fun upload(data: UByteArray): AGBuffer =
52+
upload(Uint8Buffer(data).buffer, needClone = false)
53+
54+
fun upload(data: FloatArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
55+
upload(Float32Buffer(data, offset, length).buffer, needClone = false)
56+
57+
fun upload(data: IntArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
58+
upload(Int32Buffer(data, offset, length).buffer, needClone = false)
59+
60+
fun upload(data: ShortArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
61+
upload(Int16Buffer(data, offset, length).buffer, needClone = false)
62+
63+
fun upload(data: Buffer, offset: Int, length: Int = data.size - offset): AGBuffer =
64+
upload(data.sliceWithSize(offset, length), needClone = true)
65+
66+
fun upload(data: Buffer): AGBuffer =
67+
upload(data, needClone = true)
68+
69+
private fun upload(data: Buffer, needClone: Boolean): AGBuffer {
70+
if (mem?.sizeInBytes == data.sizeInBytes) {
71+
// Do not mark as dirty if the data is the same
72+
if (data.sizeInBytes < 1024 && Buffer.equals(data, 0, mem!!, 0, data.sizeInBytes)) {
73+
return this
74+
}
75+
76+
Buffer.copy(data, 0, mem!!, 0, data.sizeInBytes)
77+
} else {
78+
mem = if (needClone) data.clone() else data
5979
}
60-
//println("New Data!")
61-
mem = data.clone()
80+
6281
markAsDirty()
6382
return this
6483
}

0 commit comments

Comments
 (0)