Android - OpenGL FloatBuffer 与 IntBuffer

标签 android performance opengl-es

在大多数 Android 设备上,如果我用整数而不是 float 进行所有 OpenGL 顶点计算/渲染,我是否应该期待性能提升?

我最近从使用 0:width, 0:height 而不是 -1:1, -1:1 的 OpenGL viewport 切换到 OpenGL viewport,这样我就可以将我所有的绘图计算/缓冲区转换为 Ints 而不是 Floats 如果它对性能是可取的。

例如,如果我在我的应用中执行许多以下类型的计算和渲染。

float x1 = someFloatCalculation(foo);
float x2 = someFloatCalculation(bar);
float y1 = someOtherFloatCalculation(foo);
float y2 = someOtherFloatCalculation(bar);
// the float buffer for the coordinates
FloatBuffer buf = makeFloatBuffer(new float[] { x1, y1, x2, y1, x1,
                                                y2, x2, y2 });
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, buf);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

是否可以通过更改为类似的东西来加快速度

int x1 = someIntCalculation(foo);
int x2 = someIntCalculation(bar);
int y1 = someOtherIntCalculation(foo);
int y2 = someOtherIntCalculation(bar);
// the float buffer for the coordinates
IntBuffer buf = makeIntBuffer(new int[] { x1, y1, x2, y1, x1,
                                            y2, x2, y2 });
// GL10.GL_INT is not an existing GL10 constant.  What to use instead?
gl.glVertexPointer(2, ???GL10.GL_INT???, 0, buf);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

请注意这两个示例中的计算将完全相同,只是后一个版本只是将结果四舍五入为最接近的整数。

一个相关的问题是,如果我的视口(viewport)是从 0 到宽度和 0 到高度(以像素为单位),那么我是否会通过舍入在渲染中失去任何平滑度。也就是说,如果我用浮点值绘制一条线,OpenGL 将 float “四舍五入”到最近的像素是否会与四舍五入到最近的 int 然后渲染有相似的结果?

最佳答案

On most Android devices, should I expect a performance increase if I do all my OpenGL vertex calculations/rendering in Integers instead of Floats?

不,您不会看到任何区别。 GPU 是为浮点运算而生的。最有可能发生的是,您的整数甚至在上传到 GPU 之前就被转换为 float 。整数函数在那里是为了方便,但它在渲染端都是 float 。

关于Android - OpenGL FloatBuffer 与 IntBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10256041/

相关文章:

android - 静态类变量的生命周期

javascript - 在沉浸模式下使用 cordova 的 Android 中的黑 strip 有缺口

相当于 textCapSentences 的 android TextView

Android 许可应用程序显示未许可?

java - 提高 javafx 性能的建议

java - Android OpenGL 纹理/对象检测

opengl - 为几何吞吐量调整 OpenGL 性能

java - 如何在迭代时向作为 HashMap 值的 ArrayList 添加内容?

java - 如何在OpenGL中移动2D相机

android - 轻量级跨平台C GUI(OpenGL)