Why can we directly allocate bytes in a ByteBuffer, but not floats in a FloatBuffer

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

ByteBuffer bf ;
bf.allocateDirect();

      

But we cannot do this in the case of FloatBuffer, it is not available, why?

I was wondering if this is because of:

A byte is available in hardware (since OpenGL only works on hardware as opposed to delvik) and registers on hardware (GPU hardware) are in bytes, even floating point numbers must be stored in a 4-byte register, which may not be available. therefore, we cannot allocate directly, but we must tell the buffers to allocate memory for a block of a given size and then put the data into these blocks and process it again as a FloatBuffer.

+3


source to share


1 answer


OpenGL es is written in c. In c, floats, integers, etc. Not fixed size like java. The floating point number in java is 32 bits. Now let's see how java uses opengl. When you send vertices to the graphics pipeline with java, you are actually calling c functions that do the dirty work for you. This is called ndk and you will find more information here: https://developer.android.com/tools/sdk/ndk/index.html . C translates to assembly code, so each float can have a different byte size on each phone depending on the processor architecture. You are using nio buffers (more details here: https://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html) to ensure that your floating point array sizes are based on your phone processor architecture (custom order) and not jvm's fixed primitive sizes. Finally, imagine you have a java float vertex array (32bit fixed size). Floating processors 64 bit. If you call opengl es function from java, your program will fail. Hope I helped.



+3


source







All Articles