Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions korge-core/src/korlibs/graphics/AGObjects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,39 @@ class AGBuffer : AGObject() {
// @TODO: Allow upload range in addition to the full buffer.
// @TODO: This will allow to upload chunks of uniform buffers for example.
// glBufferData & glBufferSubData
fun upload(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Int8Buffer(data, offset, length).buffer)
fun upload(data: UByteArray): AGBuffer = upload(Uint8Buffer(data).buffer)
fun upload(data: FloatArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Float32Buffer(data, offset, length).buffer)
fun upload(data: IntArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Int32Buffer(data, offset, length).buffer)
fun upload(data: ShortArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer = upload(Int16Buffer(data, offset, length).buffer)
fun upload(data: Buffer, offset: Int, length: Int = data.size - offset): AGBuffer = upload(data.sliceWithSize(offset, length))
fun upload(data: Buffer): AGBuffer {
//println(data.sizeInBytes)
// Only check small buffers
if (data.sizeInBytes < 1024) {
if (this.mem != null && this.mem!!.sizeInBytes == data.sizeInBytes && arrayequal(this.mem!!, 0, data, 0, data.sizeInBytes)) return this
Copy link
Contributor

@soywiz soywiz May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part deleted? I guess the point is not being marked as dirty, so it doesn't need to be uploaded to the GPU again.

fun upload(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
upload(Int8Buffer(data, offset, length).buffer, needClone = false)

fun upload(data: UByteArray): AGBuffer =
upload(Uint8Buffer(data).buffer, needClone = false)

fun upload(data: FloatArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
upload(Float32Buffer(data, offset, length).buffer, needClone = false)

fun upload(data: IntArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
upload(Int32Buffer(data, offset, length).buffer, needClone = false)

fun upload(data: ShortArray, offset: Int = 0, length: Int = data.size - offset): AGBuffer =
upload(Int16Buffer(data, offset, length).buffer, needClone = false)

fun upload(data: Buffer, offset: Int, length: Int = data.size - offset): AGBuffer =
upload(data.sliceWithSize(offset, length), needClone = true)

fun upload(data: Buffer): AGBuffer =
upload(data, needClone = true)

private fun upload(data: Buffer, needClone: Boolean): AGBuffer {
if (mem?.sizeInBytes == data.sizeInBytes) {
Copy link

Copilot AI May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a check to compare the content of mem and data for small buffers, as the previous implementation did, to avoid unnecessary copying when the existing buffer already matches the new data.

Copilot uses AI. Check for mistakes.
// Do not mark as dirty if the data is the same
if (data.sizeInBytes < 1024 && Buffer.equals(data, 0, mem!!, 0, data.sizeInBytes)) {
return this
}

Buffer.copy(data, 0, mem!!, 0, data.sizeInBytes)
} else {
mem = if (needClone) data.clone() else data
}
//println("New Data!")
mem = data.clone()

markAsDirty()
return this
}
Expand Down
Loading