Hello Guest

can you recommend a nio buffer based matrix4 math library

  • 2 Replies
  • 3245 Views
can you recommend a nio buffer based matrix4 math library
« on: November 22, 2014, 12:45:51 »
as I understand the JVM specs, arrays need not be contiguous(!?), does anyone know is there a way you can make a buffer and an array where the elements use the same physical memory?

Is there a performance hit on buffers get(n) / put(n) am I better just using an array or set of floats anyhow and just creating a buffer once all my calculations are done? (if so is it as well to reused a buffer and put and array to it)

I can always hack together the bit of matrix4 math I need for myself, but I want to make sure its using the most efficient method either when calculating or when passing to GL which ever is going to have the best impact

tia

*

Offline Cornix

  • *****
  • 488
Re: can you recommend a nio buffer based matrix4 math library
« Reply #1 on: November 22, 2014, 14:21:25 »
I have tested this once. Using arrays is a bit faster then using Buffers. Using the bulk put / get methods of buffers (that use arrays) is much faster then using the put / get that takes a single element.
But you should always check for yourself. Writing a simple test should take no longer then a few minutes.

*

Offline spasi

  • *****
  • 2261
    • WebHotelier
Re: can you recommend a nio buffer based matrix4 math library
« Reply #2 on: November 22, 2014, 14:53:43 »
No, it is not possible for arrays and buffers to share the same physical memory. Arrays are stored on the heap and buffers off-heap. The reason LWJGL uses buffers is because arrays can be moved at any time by the garbage collector and there is no way to restrict native code from accessing the old location. Well, in theory you can, using JNI's GetPrimitiveArrayCritical/ReleasePrimitiveArrayCritical, but that has serious GC implications and the JVM often has to copy data back and forth. You're correct that array storage is unspecified and may not be contiguous, but in practice they always are, so that's not the primary reason for not using them.

One of the possibilities for Arrays 2.0 is support for off-heap storage, but I wouldn't hold my breath for it (earliest we could hope for is Java 10).

Buffer access is optimized like array access by the JVM, more or less. In practice, array access may be slightly faster and both are slower than object field access. My recommendation would be:

- For heavy computations, use plain Java objects/arrays, copy to buffer as the final step.
- For simple computations, use buffers directly.

Or use OpenCL or OpenGL compute shaders for computations! :)