Skip to content

Commit 1cf79c0

Browse files
emyfopsAvanatiker
authored andcommitted
Buffer bind block
1 parent 3828a49 commit 1cf79c0

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

common/src/main/kotlin/com/lambda/graphics/buffer/Buffer.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,23 @@ abstract class Buffer(
113113
private val bufferIds = IntArray(buffers)
114114

115115
/**
116-
* Binds the buffer id to the [target].
116+
* Execute the [block] in a bound context
117117
*/
118-
open fun bind(id: Int) = glBindBuffer(target, id)
118+
fun bind(block: Buffer.() -> Unit) {
119+
bind()
120+
block(this)
121+
bind(0)
122+
}
119123

120124
/**
121-
* Binds current the buffer [index] to the [target].
125+
* Binds the buffer id to the [target].
122126
*/
123-
fun bind() = bind(bufferAt(index))
127+
open fun bind(id: Int) = glBindBuffer(target, id)
124128

125129
/**
126-
* Returns the id of the buffer based on the index.
130+
* Binds current the buffer [index] to the [target].
127131
*/
128-
fun bufferAt(index: Int) = bufferIds[index]
132+
fun bind() = bind(bufferIds[index])
129133

130134
/**
131135
* Swaps the buffer [index] if [buffers] is greater than 1.

common/src/main/kotlin/com/lambda/graphics/buffer/pixel/PixelBuffer.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
package com.lambda.graphics.buffer.pixel
1919

2020
import com.lambda.graphics.buffer.Buffer
21-
import com.lambda.graphics.gl.putTo
2221
import com.lambda.graphics.texture.Texture
23-
import com.lambda.threading.runSafeGameScheduled
2422
import com.lambda.util.math.MathUtils.toInt
2523
import org.lwjgl.opengl.GL45C.*
2624
import java.nio.ByteBuffer

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ class VertexArray(
3737
indicesSize: Long,
3838
indicesPointer: Long,
3939
verticesOffset: Int
40-
) {
41-
bind()
42-
glDrawElementsBaseVertex(
43-
vertexMode.mode,
44-
indicesSize.toInt() / UInt.SIZE_BYTES,
45-
GL_UNSIGNED_INT,
46-
indicesPointer,
47-
verticesOffset / attributes.stride
48-
)
49-
bind(0)
50-
}
40+
) = bind {
41+
glDrawElementsBaseVertex(
42+
vertexMode.mode,
43+
indicesSize.toInt() / UInt.SIZE_BYTES,
44+
GL_UNSIGNED_INT,
45+
indicesPointer,
46+
verticesOffset / attributes.stride
47+
)
48+
}
5149

5250
override fun map(size: Long, offset: Long, block: (ByteBuffer) -> Unit) = throw UnsupportedOperationException()
5351
override fun upload(data: ByteBuffer, offset: Long) = throw UnsupportedOperationException()

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,7 @@ class PersistentBuffer(
9595
cacheSize = 0
9696
}
9797

98-
fun use(block: () -> Unit) {
99-
glBuffer.bind()
100-
block()
101-
glBuffer.bind(0)
102-
}
98+
fun use(block: () -> Unit) = glBuffer.bind { block() }
10399

104100
private fun memcmp(a: ByteBuffer, b: ByteBuffer, pointer: Int, size: Int): Boolean {
105101
for (i in pointer..<(pointer + size)) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ class VertexPipeline(
145145
}
146146

147147
init {
148-
vao.bind()
149-
vbo.use(attributes::link)
150-
vao.bind(0)
148+
vao.bind { vbo.use(attributes::link) }
151149
}
152150
}

0 commit comments

Comments
 (0)