Android - OpenGL FloatBuffer与IntBuffer

时间:2012-04-21 03:53:23

标签: android performance opengl-es

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

我最近使用的是OpenGL视口0:width,0:height而不是-1:1,-1:1,所以我可以将所有绘图计算/缓冲区转换为Ints而不是Floats if性能是可取的。

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

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);

注意两个示例中的计算完全等效,但后者版本只是将结果舍入到最近的int。

一个相关的问题是,如果我的视口是从0到0和从0到高(以像素为单位),我是否会通过舍入来消除渲染中的任何平滑度。也就是说,如果我使用浮点值绘制一条直线,OpenGL将浮点数“舍入”到最近的像素会产生类似于舍入到最近的int然后渲染的结果吗?

1 个答案:

答案 0 :(得分:6)

  

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

不,你会看到没有区别。 GPU用于浮点运算。最有可能发生的是,在将整数上传到GPU之前,您的整数会转换为浮点数。整数函数是为了方便起见,但它在渲染方面都是浮点数。

相关问题