Why can we directly allocate bytes in ByteBuffer but not floating Point in FloatBuffer

519 views Asked by At

In java using openGL (ES) we can directly allocate ByteBuffer like

ByteBuffer bf ;
bf.allocateDirect();

But we can not do that in case of FloatBuffer it is not aviliable , why is that ?

I was wondering if it is because of :

Byte is accessible in hardware level (as OpenGL works just above hardware unlike delvik ) and registers in hardware (hardware of GPU ) are in bytes , even floating points numbers should be stored in 4 byte register which may not be available so we cannot allocate directly , rather we should tell the buffer to allocate the memory for a block of given size and after that put the data in those blocks and treat it again as FloatBuffer.

1

There are 1 answers

2
KostasRim On BEST ANSWER

OpenGL es is written in c. In c floats, integers etc are not fixed size like java. A float point number in java is 32 bits. Now lets examine how java uses opengl es. When you send vertices to the graphics pipeline using java you actually call c functions that do the dirty work for you. This is called ndk and you find more info here : https://developer.android.com/tools/sdk/ndk/index.html. C is translated to assembly code thus each float can have different byte size on each phone depending on cpu architecture. You use nio buffers ( more here : https://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html ) to assure that your float array sizes are BASED on your phone's cpu architecture(native order) and not on jvm fixed primitive sizes. Lastly, imagine you have a vertices array of java floats (32bit fixed size). Your cpu floats are 64 bit. If you call an opengl es function from java your program will end up crashing. Hope i helped.