Skip to content

Commit

Permalink
FragmentedArrayFloatList: allow batch adding
Browse files Browse the repository at this point in the history
Will be complete in kutil 1.24
  • Loading branch information
Bixilon committed Oct 10, 2023
1 parent 0a06695 commit d01ac89
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@ class SingleChunkMesh(context: RenderContext, initialCacheSize: Int, onDemand: B
val transformedUV = texture.renderData.transformUV(uv).array
data.add(position)
data.add(transformedUV)
data.add(texture.renderData.shaderTextureId.buffer())
data.add(((light shl 24) or tintColor).buffer())
data.add(
texture.renderData.shaderTextureId.buffer(),
(((light shl 24) or tintColor).buffer())
)
}

fun addVertex(x: Float, y: Float, z: Float, uv: FloatArray, texture: Texture, shaderTextureId: Float, lightTint: Float) {
data.ensureSize(WorldMeshStruct.FLOATS_PER_VERTEX)
val transformedUV = texture.renderData.transformUV(uv)
data.add(x)
data.add(y)
data.add(z)
data.add(x, y, z)
data.add(transformedUV)
data.add(shaderTextureId)
data.add(lightTint)
data.add(shaderTextureId, lightTint)
}

override fun compareTo(other: SingleChunkMesh): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class FragmentedArrayFloatList(
}

private fun grow(size: Int): FloatBuffer {
checkFinished()
if (finished) throw IllegalStateException()
if (limit - this.size >= size) {
return this.incomplete.first()
return this.incomplete[0]
}
val grow = if (nextGrowStep < size) {
(size / nextGrowStep + 1) * nextGrowStep
Expand All @@ -78,6 +78,27 @@ class FragmentedArrayFloatList(
invalidateOutput()
}

fun add(value1: Float, value2: Float) {
var buffer = grow(2)
buffer.put(value1); if (tryPush(buffer)) buffer = grow(1)
buffer.put(value2)

size += 2
tryPush(buffer)
invalidateOutput()
}

fun add(value1: Float, value2: Float, value3: Float) {
var buffer = grow(3)
buffer.put(value1); if (tryPush(buffer)) buffer = grow(2)
buffer.put(value2); if (tryPush(buffer)) buffer = grow(1)
buffer.put(value3)

size += 3
tryPush(buffer)
invalidateOutput()
}

private fun tryPush(fragment: FloatBuffer): Boolean {
if (fragment.position() != fragment.limit()) {
return false
Expand Down

0 comments on commit d01ac89

Please sign in to comment.