java 为什么是 FloatBuffer 而不是 float[]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10697161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why FloatBuffer instead of float[]?
提问by Tim
I've been using FloatBuffers in my Android code for a while (copied it from some opengles tutorial), but I cannot understand exactly what this construct is and why it is needed.
我一直在我的 Android 代码中使用 FloatBuffers 一段时间(从一些 opengles 教程中复制它),但我无法确切理解这个构造是什么以及为什么需要它。
For example this code (or similar) I see in many many peoples' code and android tutorials:
例如,我在许多人的代码和 android 教程中看到的这段代码(或类似代码):
float[] vertices = ...some array...
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // use the device hardware's native byte order
FloatBuffer fb = vbb.asFloatBuffer(); // create a floating point buffer from the ByteBuffer
fb.put(vertices); // add the coordinates to the FloatBuffer
fb.position(0); // set the buffer to read the first coordinate
This seems awfully verbose and messy for something which as far as I can tell is just a fancy wrapper around of an array of floats.
对于某些东西来说,这似乎非常冗长和凌乱,据我所知,这只是一系列浮点数的花哨包装。
Questions:
问题:
What is the justification for this type of class (ByteBuffer, FloatBuffer), as opposed to any other kind of collection or simple array of floats?
What's the idea behind creating a ByteBuffer before converting it into a FloatBuffer?
与任何其他类型的集合或简单的浮点数组相比,这种类型的类(ByteBuffer、FloatBuffer)的理由是什么?
在将其转换为 FloatBuffer 之前创建 ByteBuffer 背后的想法是什么?
采纳答案by mikera
The main reason is performance: ByteBuffers and the other NIO classes enable accelerated operations when interfacing with native code (typically by avoiding the need to copy data into a temporary buffer).
主要原因是性能:ByteBuffers 和其他 NIO 类在与本机代码接口时启用加速操作(通常是通过避免将数据复制到临时缓冲区的需要)。
This is pretty important if you are doing a lot of OpenGL rendering calls for example.
例如,如果您正在执行大量 OpenGL 渲染调用,则这一点非常重要。
The reason for creating a ByteBuffer first is that you want to use the allocateDirect call to create a directbyte buffer, which benefits from the accelerated operations. You then create a FloatBuffer from this that shares the same memory. The FloatBuffer doesn't itself have an allocateDirect method for some reason, which is why you have to go via ByteBuffer.
首先创建 ByteBuffer 的原因是您想使用allocateDirect 调用创建一个直接字节缓冲区,这受益于加速操作。然后,您从中创建一个共享相同内存的 FloatBuffer。由于某种原因,FloatBuffer 本身没有allocateDirect 方法,这就是您必须通过ByteBuffer 的原因。
回答by Anchal
We do our coding in Java on Android, but the underlying implementation of OpenGL ES is actually written in C. Before we pass our data to OpenGL, we need to convert it into a form that it's going to understand. Java and the native system might not store their bytes in the same order, so we use a special set of buffer classes and create a ByteBuffer large enough to hold our data, and tell it to store its data using the native byte order. We then convert it into a FloatBuffer so that we can use it to hold floating-point data. Finally, we copy our array into the buffer.
我们在 Android 上用 Java 进行编码,但 OpenGL ES 的底层实现实际上是用 C 编写的。在我们将数据传递给 OpenGL 之前,我们需要将其转换为它会理解的形式。Java 和本机系统可能不会以相同的顺序存储它们的字节,因此我们使用一组特殊的缓冲区类并创建一个足够大的 ByteBuffer 来保存我们的数据,并告诉它使用本机字节顺序存储数据。然后我们将其转换为 FloatBuffer,以便我们可以使用它来保存浮点数据。最后,我们将数组复制到缓冲区中。