@@ -6,6 +6,15 @@ import org.lwjgl.opengl.GL
66import org.lwjgl.opengl.GL45C.*
77import java.nio.ByteBuffer
88
9+ /* *
10+ * Represents a Pixel Buffer Object (PBO) that facilitates asynchronous data transfer to the GPU.
11+ * This class manages the creation, usage, and cleanup of PBOs and provides methods to map textures and upload data efficiently.
12+ *
13+ * @property width The width of the texture in pixels.
14+ * @property height The height of the texture in pixels.
15+ * @property buffers The number of PBOs to be used. Default is 2, which allows double buffering.
16+ * @property bufferUsage The usage pattern of the buffer, indicating how the buffer will be used (static, dynamic, etc.).
17+ */
918class PixelBuffer (
1019 private val width : Int ,
1120 private val height : Int ,
@@ -24,6 +33,12 @@ class PixelBuffer(
2433
2534 private var initialDataSent: Boolean = false
2635
36+ /* *
37+ * Maps the given texture ID to the buffer and performs the necessary operations to upload the texture data.
38+ *
39+ * @param id The texture ID to which the buffer will be mapped.
40+ * @param buffer The [ByteBuffer] containing the pixel data to be uploaded to the texture.
41+ */
2742 fun mapTexture (id : Int , buffer : ByteBuffer ) {
2843 if (! initialDataSent) {
2944 glBindTexture(GL_TEXTURE_2D , id)
@@ -59,6 +74,12 @@ class PixelBuffer(
5974 }
6075 }
6176
77+ /* *
78+ * Uploads the given pixel data to the PBO and executes the provided processing function to manage the PBO's data transfer.
79+ *
80+ * @param data The [ByteBuffer] containing the pixel data to be uploaded.
81+ * @param process A lambda function to execute after uploading the data to manage the PBO's data transfer.
82+ */
6283 fun upload (data : ByteBuffer , process : () -> Unit ) =
6384 recordTransfer {
6485 if (buffers >= 2 )
@@ -95,6 +116,11 @@ class PixelBuffer(
95116 writeIdx = uploadIdx
96117 }
97118
119+ /* *
120+ * Measures and records the time taken to transfer data to the PBO, calculating the transfer rate in bytes per second.
121+ *
122+ * @param block A lambda function representing the block of code where the transfer occurs.
123+ */
98124 private fun recordTransfer (block : () -> Unit ) {
99125 // Start the timer
100126 glBeginQuery(GL_TIME_ELAPSED , queryId)
@@ -110,12 +136,19 @@ class PixelBuffer(
110136 if (time > 0 ) transferRate = (width * height * 4L * 1_000_000_000 ) / time
111137 }
112138
113- // Called when no references to the object exist
139+ /* *
140+ * Cleans up resources by deleting the PBOs when the object is no longer in use.
141+ */
114142 fun finalize () {
115143 // Delete the PBOs
116144 glDeleteBuffers(pboIds)
117145 }
118146
147+ /* *
148+ * Initializes the PBOs, allocates memory for them, and handles unsupported PBO scenarios.
149+ *
150+ * @throws IllegalArgumentException If the number of buffers is less than 0.
151+ */
119152 init {
120153 if (buffers < 0 ) throw IllegalArgumentException (" Buffers must be greater than or equal to 0" )
121154
0 commit comments