On Nov 23, 5:46 pm, Bipin George Mathew <
bipi...@gmail.com> wrote:
> Given this, why do APIs (like the opengl wrappers) that need to pass
> float arrays prefer method 1 (direct float buffer) over method 2?
I don't know much about OpenGL, so I can't give you a definite
answer. I can speculate a little, and hopefully someone will correct
me if I'm off track.
Generally speaking, direct byte buffers are cheaper to access from
native code. If we assume that the underlying OpenGL implementation
is written in C/C++, and uses JNI like everybody else, then using a
direct byte buffer allows OpenGL to access the data you provide
without involving the VM. If you used a float[] on the virtual heap,
it would have to use one of the primitive array calls to access the
data, guaranteeing that on certain VMs the data will be copied. Not
something you want to bake into the API itself.
If the geometry in the buffers can be re-used, you only need to copy
it once, and OpenGL can read it multiple times. (Again, possible
display of ignorance here.)
(It's also worth noting that direct buffers allow you to use data from
"native" sources. For example, you can use the JNI
NewDirectByteBuffer call to allocate a "direct" ByteBuffer that points
to memory mapped directly from a file. This can be very convenient in
some situations. Probably not important for geometry.)
The trade-off is that it's awkward and more expensive to manipulate
data in Java. Without a notion like "unsafe" arrays, I don't think
it's possible to have something that works efficiently on both sides
and never has to copy data.