Hello Guest

glNamedBufferStorage not working as expected

  • 2 Replies
  • 110 Views
glNamedBufferStorage not working as expected
« on: March 30, 2018, 20:55:53 »
Hi,

I'm currently playing a bit with Lwjgl while I'm learning Kotlin.

I'm experiencing some issues with the glNamedBufferStorage function. Whenever I try to fill the buffer using a FloatBuffer, nothing appears on the screen.

The problem is that it's only due to the fact I'm using a FloatBuffer, because when I use a FloatArray, it works as intended. Does anyone have an idea about what I'm missing ?

Here is the code where the problem appears, with the two version :
Code: [Select]
        val a = FloatArray(data.size * componentCount)
        val buffer: FloatBuffer = FloatBuffer.allocate(data.size * componentCount)

        data.forEach {
            buffer.put(it.x)
            buffer.put(it.y)
            buffer.put(it.z)
        }
        buffer.flip()

        data.forEachIndexed() {
            index, data ->
                a[componentCount * index] = data.x
                a[componentCount * index + 1] = data.y
                a[componentCount * index + 2] = data.z
        }

        GL45.glNamedBufferStorage(id, buffer, flags) // This does not work
        GL45.glNamedBufferStorage(id, a, flags) // This does work

If you need anything else, I'll give it.
Thanks

*

Offline KaiHH

  • **
  • 55
Re: glNamedBufferStorage not working as expected
« Reply #1 on: March 30, 2018, 21:01:50 »
You must only use direct NIO buffers, not ones backed by arrays. LWJGL provides a very convenient BufferUtils class to allocate such buffers.

If you want to delve deeper into the topic of memory management in LWJGL3, read this nice blog entry: https://blog.lwjgl.org/memory-management-in-lwjgl-3/
« Last Edit: March 30, 2018, 21:18:19 by KaiHH »

Re: glNamedBufferStorage not working as expected
« Reply #2 on: March 30, 2018, 21:22:41 »
Thanks

I tried with direct NIO buffers but the result is the same, nothing appears on the screen.
I checked that the buffer was direct, so the problem might elsewhere.
I tried using FloatBuffer allocated using BufferUtils.createFloatBuffer, do I need to do something else for it to work ?

Here is the code
Code: [Select]
val buffer = BufferUtils.createFloatBuffer(data.size * componentCount)
        println(buffer.isDirect)

        data.forEach {
            buffer.put(it.x)
            buffer.put(it.y)
            buffer.put(it.z)
        }
        buffer.flip()

        data.forEachIndexed() {
            index, data ->
                a[componentCount * index] = data.x
                a[componentCount * index + 1] = data.y
                a[componentCount * index + 2] = data.z
        }

        GL45.glNamedBufferStorage(id, buffer, flags)

EDIT: Everything is working, I did a beginner mistake. Thank you for your help :)
« Last Edit: March 30, 2018, 21:41:45 by Corendos »