Skip to content

Commit ea972be

Browse files
committed
todo: pixel buffer object
1 parent 81a0c63 commit ea972be

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.lambda.graphics.buffer
2+
3+
import org.lwjgl.opengl.GL45C.*
4+
import java.nio.ByteBuffer
5+
6+
// NOT TESTED
7+
class PixelBuffer(
8+
width: Int,
9+
height: Int
10+
) {
11+
private val pboIds = IntArray(2) { 0 }
12+
private var index = 0
13+
14+
fun upload(data: ByteBuffer, block: () -> Unit) {
15+
// Bind the current PBO for writing
16+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[index])
17+
18+
// Map the buffer and copy data into it
19+
val bufferData = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY) as ByteBuffer
20+
bufferData.put(data)
21+
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)
22+
23+
// Process the data
24+
block()
25+
26+
// Unbind the buffer
27+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0)
28+
29+
// Switch to the other PBO
30+
index = (index + 1) % 2
31+
}
32+
33+
fun download(): ByteBuffer {
34+
// Bind the current PBO for reading
35+
glBindBuffer(GL_PIXEL_PACK_BUFFER, pboIds[index])
36+
37+
// Map the buffer and copy data from it
38+
val bufferData = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY) as ByteBuffer
39+
val data = bufferData.slice()
40+
41+
// Unbind the buffer
42+
glUnmapBuffer(GL_PIXEL_PACK_BUFFER)
43+
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0)
44+
45+
return data
46+
}
47+
48+
fun finalize() {
49+
// Delete the PBOs
50+
glDeleteBuffers(pboIds)
51+
}
52+
53+
init {
54+
// Generate the PBOs
55+
glGenBuffers(pboIds)
56+
57+
// Fill the buffers with null data to allocate the memory spaces
58+
glBindBuffer(GL_PIXEL_PACK_BUFFER, pboIds[0])
59+
glBufferData(GL_PIXEL_PACK_BUFFER, width * height * 4L, GL_DYNAMIC_READ)
60+
glBindBuffer(GL_PIXEL_PACK_BUFFER, pboIds[1])
61+
glBufferData(GL_PIXEL_PACK_BUFFER, width * height * 4L, GL_DYNAMIC_READ)
62+
63+
// Unbind the buffer
64+
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0)
65+
}
66+
}

0 commit comments

Comments
 (0)