@@ -17,14 +17,24 @@ class PixelBuffer(
1717 private var uploadIdx = 0 // Used to upload data to the PBO
1818
1919 private val queryId = glGenQueries() // Used to measure the time taken to upload data to the PBO
20- val uploadTime get() = IntArray (1 ).also { glGetQueryObjectiv(queryId, GL_QUERY_RESULT , it) }[0 ]
21- var transferRate = 0L // The transfer rate in bytes per second
22- private set
20+ private val uploadTime get() = IntArray (1 ).also { glGetQueryObjectiv(queryId, GL_QUERY_RESULT , it) }[0 ]
21+ private var transferRate = 0L // The transfer rate in bytes per second
2322
24- var pboSupported = false
25- private set
23+ private val pboSupported = GL .getCapabilities().OpenGL30 || GL .getCapabilities().GL_ARB_pixel_buffer_object
24+
25+ private var initialDataSent: Boolean = false
26+
27+ fun mapTexture (id : Int , buffer : ByteBuffer ) {
28+ if (! initialDataSent) {
29+ glBindTexture(GL_TEXTURE_2D , id)
30+ glTexImage2D(GL_TEXTURE_2D , 0 , GL_RGBA8 , width, height, 0 , GL_RGBA , GL_UNSIGNED_BYTE , 0 )
31+ glBindTexture(GL_TEXTURE_2D , 0 )
32+
33+ initialDataSent = true
34+
35+ return
36+ }
2637
27- fun mapTexture (id : Int , buffer : ByteBuffer ) =
2838 upload(buffer) {
2939 // Bind the texture
3040 glBindTexture(GL_TEXTURE_2D , id)
@@ -34,13 +44,20 @@ class PixelBuffer(
3444 glBindBuffer(GL_PIXEL_UNPACK_BUFFER , pboIds[writeIdx])
3545
3646 // Perform the actual data transfer to the GPU
37- glTexSubImage2D (GL_TEXTURE_2D , 0 , 0 , 0 , width, height, GL_RGBA , GL_UNSIGNED_BYTE , 0 )
47+ glTexImage2D (GL_TEXTURE_2D , 0 , GL_RGBA8 , width, height, 0 , GL_RGBA , GL_UNSIGNED_BYTE , 0 )
3848 }
3949 else {
4050 // Perform the actual data transfer to the GPU
41- glTexSubImage2D (GL_TEXTURE_2D , 0 , 0 , 0 , width, height, GL_RGBA , GL_UNSIGNED_BYTE , buffer)
51+ glTexImage2D (GL_TEXTURE_2D , 0 , GL_RGBA8 , width, height, 0 , GL_RGBA , GL_UNSIGNED_BYTE , buffer)
4252 }
53+
54+ glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR )
55+ glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR )
56+
57+ // Unbind the texture
58+ glBindTexture(GL_TEXTURE_2D , 0 )
4359 }
60+ }
4461
4562 fun upload (data : ByteBuffer , process : () -> Unit ) =
4663 recordTransfer {
@@ -50,8 +67,6 @@ class PixelBuffer(
5067 // Copy the pixel values from the PBO to the texture
5168 process()
5269
53- if (! pboSupported) return @recordTransfer
54-
5570 // Bind the current PBO for writing
5671 glBindBuffer(GL_PIXEL_UNPACK_BUFFER , pboIds[uploadIdx])
5772
@@ -81,11 +96,6 @@ class PixelBuffer(
8196 }
8297
8398 private fun recordTransfer (block : () -> Unit ) {
84- if (! pboSupported) {
85- block()
86- return
87- }
88-
8999 // Start the timer
90100 glBeginQuery(GL_TIME_ELAPSED , queryId)
91101
@@ -97,9 +107,7 @@ class PixelBuffer(
97107
98108 // Calculate the transfer rate
99109 val time = uploadTime
100- if (time > 0 ) {
101- transferRate = (width * height * 4L * 1_000_000_000 ) / time
102- }
110+ if (time > 0 ) transferRate = (width * height * 4L * 1_000_000_000 ) / time
103111 }
104112
105113 // Called when no references to the object exist
@@ -109,14 +117,11 @@ class PixelBuffer(
109117 }
110118
111119 init {
112- // Check if the PBO is supported
113- GL .getCapabilities().let { pboSupported = it.OpenGL30 || it.GL_ARB_pixel_buffer_object }
120+ if (buffers < 0 ) throw IllegalArgumentException (" Buffers must be greater than or equal to 0" )
114121
115122 if (! pboSupported && buffers > 0 )
116123 LOG .warn(" Client tried to utilize PBOs, but they are not supported on the machine, falling back to direct buffer upload" )
117124
118- if (buffers < 0 ) throw IllegalArgumentException (" Buffers must be greater than or equal to 0" )
119-
120125 // Generate the PBOs
121126 glGenBuffers(pboIds)
122127
0 commit comments