Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  What excactly is a FloatBuffer?  (Read 2156 times)
0 Members and 1 Guest are viewing this topic.
Offline The Lion King
« Posted 2014-03-17 04:57:31 »

Before you spam "its a thing you need to send data to openGL" i know that.

a direct floatbuffer apparently speeds up native function calls, why? What is the difference between a float[] and a FloatBuffer under the hood?

also, I did a test in filling float[10000000]s and float buffers using different methods.

for(int i = 0; i < 10000000; i++)
array[i] = .5f;

took 47 ms

System.arraycopy took >1 ms

FloatBuffer.put(float[]) took 15ms
FloatBuffer.put(FloatBuffer) took 15ms

re-did test with an array that is 10x as big, made the time diff more noticible

"You have to want it more than you want to breath, then you will be successful"
Offline HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

« Reply #1 - Posted 2014-03-17 05:08:03 »

FloatBuffers allow better access from native code.

Offline The Lion King
« Reply #2 - Posted 2014-03-17 05:14:06 »

thanks, but the question is a little deeper than that. How does a FloatBuffer allow for better access by native than a float[]?

"You have to want it more than you want to breath, then you will be successful"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom_mai78101
« Reply #3 - Posted 2014-03-17 13:26:30 »

This is pure speculation from my part.

In Java, float[] points to a reference generated by bytecode. Since you can use float[].length, it probably makes float[] a class rather than an actual float array.

So, a FloatBuffer generates a memory location where it's an actual byte array generated by bytecode. If anything were to read that FloatBuffer, it reads the byte array as a float array.
Offline TeamworkGuy2

Junior Devvie

Medals: 10

« Reply #4 - Posted 2014-03-17 13:39:25 »

As tom_mai mentioned, a float[] is subject to the JVM's whim, it might be compacted, fragmented, or moved by the JVM or garbage collector. This means that an OpenGL driver might obtain a pointer to the array, queue a command involving the array, but then when it goes to execute the command and do something with the array, the array may have moved causing the pointer to point to an invalid memory location.

A native buffer with the correct ByteOrder, will store its contents in a fixed location in memory in the correct endian-ness of the host machine.  This allows the OS or drivers to receiver pointers to buffers and use them in the future because the location pointed to will always remain valid and will not be modified by the JVM or garbage collector.

That's my understanding after doing some research.
Offline nsigma
« Reply #5 - Posted 2014-03-17 13:39:32 »

Try reading the JavaDoc, it's what it's there for!  Wink

Essentially, a direct buffer exists (or behaves as such) outside the main Java memory heap, and in a consistent memory location.  This makes it possible for native code to read/write to the buffer without extra copying overhead.  (There are ways of pinning array memory for native calls (JNA does this) but it's much more limiting).

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline The Lion King
« Reply #6 - Posted 2014-03-17 14:54:28 »

Ok, that makes sense, thanks guys Smiley

"You have to want it more than you want to breath, then you will be successful"
Pages: [1]
  ignore  |  Print  

xxMrPHDxx (21 views)
2017-11-21 16:21:00

xxMrPHDxx (14 views)
2017-11-21 16:14:31

xxMrPHDxx (16 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (150 views)
2017-09-26 18:23:31

cybrmynd (260 views)
2017-08-02 12:28:51

cybrmynd (250 views)
2017-08-02 12:19:43

cybrmynd (247 views)
2017-08-02 12:18:09

Sralse (260 views)
2017-07-25 17:13:48

Archive (880 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!