Skip to content

Commit aa9c7ce

Browse files
committed
added glBufferStorage option
1 parent 33b291d commit aa9c7ce

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.lambda.graphics.gl.bufferBound
2121
import com.lambda.graphics.gl.bufferUsageValid
2222
import com.lambda.graphics.gl.bufferValid
2323
import org.lwjgl.opengl.GL30C.*
24+
import org.lwjgl.opengl.GL44.glBufferStorage
2425
import java.nio.ByteBuffer
2526

2627
interface IBuffer {
@@ -151,7 +152,6 @@ interface IBuffer {
151152
* @param data The data to put in the new allocated buffer
152153
*/
153154
fun allocate(data: ByteBuffer): Throwable? {
154-
// FixMe: If access contains any of GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT and the buffer was not initialized using glBufferStorage, glMapBufferRange will fail
155155
if (!bufferValid(target))
156156
return IllegalArgumentException("Target is not valid. Refer to the table in the documentation")
157157

@@ -172,17 +172,54 @@ interface IBuffer {
172172
* @param size The size of the new buffer
173173
*/
174174
fun allocate(size: Long): Throwable? {
175-
if (size < 0) return IllegalArgumentException("Invalid size parameter: $size")
175+
if (!bufferValid(target))
176+
return IllegalArgumentException("Target is not valid. Refer to the table in the documentation")
177+
178+
if (!bufferUsageValid(usage))
179+
return IllegalArgumentException("Buffer usage is invalid")
180+
181+
bind()
182+
glBufferData(target, size.coerceAtLeast(0), usage)
183+
bind(0)
184+
185+
return null
186+
}
187+
188+
/**
189+
* Create a new buffer storage
190+
* This function cannot be called twice for the same buffer
191+
* This function handles the buffer binding
192+
*/
193+
fun storage(data: ByteBuffer): Throwable? {
194+
if (!bufferValid(target))
195+
return IllegalArgumentException("Target is not valid. Refer to the table in the documentation")
196+
197+
if (!bufferUsageValid(usage))
198+
return IllegalArgumentException("Buffer usage is invalid")
199+
200+
bind()
201+
glBufferStorage(target, data, usage)
202+
bind(0)
176203

177-
// FixMe: If access contains any of GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT and the buffer was not initialized using glBufferStorage, glMapBufferRange will fail
204+
return null
205+
}
206+
207+
/**
208+
* Create a new buffer storage
209+
* This function cannot be called twice for the same buffer
210+
* This function handles the buffer binding
211+
*
212+
* @param size The size of the storage buffer
213+
*/
214+
fun storage(size: Long): Throwable? {
178215
if (!bufferValid(target))
179216
return IllegalArgumentException("Target is not valid. Refer to the table in the documentation")
180217

181218
if (!bufferUsageValid(usage))
182219
return IllegalArgumentException("Buffer usage is invalid")
183220

184221
bind()
185-
glBufferData(target, size, usage)
222+
glBufferStorage(target, size.coerceAtLeast(0), usage)
186223
bind(0)
187224

188225
return null

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ class PixelBuffer(
5454
override val bufferIds = IntArray(buffers).apply { glGenBuffers(this) }
5555

5656
private val channels = channelMapping[format] ?: throw IllegalArgumentException("Image format unsupported")
57-
private val internalFormat =
58-
reverseChannelMapping[channels] ?: throw IllegalArgumentException("Image internal format unsupported")
57+
private val internalFormat = reverseChannelMapping[channels] ?: throw IllegalArgumentException("Image internal format unsupported")
5958
private val size = width * height * channels * 1L
6059

6160
override fun upload(
@@ -121,8 +120,8 @@ class PixelBuffer(
121120
// Unbind the texture
122121
glBindTexture(GL_TEXTURE_2D, 0)
123122

124-
// Fill the buffers with null data to allocate the memory spaces
125-
allocate(size)
123+
// Fill the storage with null
124+
storage(size)
126125
}
127126

128127
companion object {

0 commit comments

Comments
 (0)