Buffer newbie

Started by ilazarte, May 12, 2010, 03:58:39

Previous topic - Next topic

ilazarte

Hi, just want to make sure I understand what buffers are and how to use them correctly.

1. As I understand it, if a buffer is 'direct' then it is created natively - i take this to mean it is allocated on the video card?
2. If one is true, for buffer values that i am changing frequently, (for example, position of a light source which is a float buffer) do i create the buffer only once, and then update it with .put and .flip every game loop?
3. Once I've allocated it (I am doing this with lwjgl BufferUtils) do I need to de-allocate it when I'm done with it?
4. One more, why do you have to .flip?

Thanks in advance.  Been making my way through Redbook+Lwjgl at the same (thanks ciardhubh!) and the combination of the two have been very powerful.   In fact, I'd say those examples should be hosted here as well! :D

Fool Running

1) A direct buffer is one that is created outside the Java VM heap but is still in the main memory (not on the video card). This allows faster access from outside applications/APIs because Java doesn't have to jump through hoops to allow the access.
2) Yes, it is better to create one buffer and re-use it.
3) You can, I think, dispose of it manually if you need to, but the garbage collector will free the buffers as well, but at "some time in the future." If you keep re-using a buffer, you shouldn't need to worry about it since you won't stop using it until the program is finished anyways.
4) Internally, a buffer is just a pointer to a memory location that was allocated for use. It can't really be used like an array. To help you with that, buffer keeps a pointer to the location where the memory was allocated, the size of the allocated memory, the amount of data in the buffer, and the current write/read location (i.e. for get() and put()s). When you add data to the buffer the write/read location is incremented to the next location. This means that if you fill the buffer, the write/read location is at the end of the buffer. When you flip() the buffer, the write/read is reset to the beginning and the amount of data in the buffer is made the limit.

Hope that helps ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

princec

Almost correct - a direct buffer does not necessarily have to exist in system RAM - the pointer can point to AGP areas, or it can even be address mapped directly to device RAM. The general gist being, it's not backed by Java heap - it can be anywhere addressable by the OS.

Cas :)

ilazarte

Thanks guys, everything I needed and more :)

Fool Running

Quote from: princec on May 12, 2010, 14:49:40
Almost correct - a direct buffer does not necessarily have to exist in system RAM - the pointer can point to AGP areas, or it can even be address mapped directly to device RAM. The general gist being, it's not backed by Java heap - it can be anywhere addressable by the OS.

Cas :)
I did not know that. Can you actually allocate memory in those locations from Java, or do the buffers have to come from external code through JNI?
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

princec

Only JNI. The classic example is the old nvidia extension for allocating VRAM and AGP RAM. All gone by the wayside now with these newfangled VBOs though.

Cas :)