Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 511 times)
0 Members and 1 Guest are viewing this topic.
Online 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.

doing
1  
2  
3  
4  
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


[edit]
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: 310
Projects: 11
Exp: 3 years


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


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

FloatBuffers allow better access from native code.

Online 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 http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#direct

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 - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!