3

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

I recently switched from using an OpenGL viewport of 0:width, 0:height instead of -1:1, -1:1, so I can get away with converting all my drawing calculations/buffers to Ints instead of Floats if it is desirable for performance.

For example, if I do many of the following type of calculations and rendering in my app.

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

Could this be sped up by changing to something like

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

Note that the calculations would be completely equivalent in the two examples, except that the latter versions would simply round the result to the nearest int.

A related question is whether I would loose any smoothness in my rendering by doing this rounding, if my viewport is from 0 to width and 0 to height (in pixels). That is, if I draw a line with float values, would OpenGL's "rounding" of a float to the nearest pixel have similar results as rounding to the nearest int and then rendering?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
khiner
  • 673
  • 8
  • 19
  • See [here](http://stackoverflow.com/questions/4364879/integer-calculations-on-gpu) or [there](http://stackoverflow.com/questions/8683720/performance-of-integer-and-bitwise-operations-on-gpu). – Stefan Hanke Apr 21 '12 at 07:06

1 Answers1

6

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

No, you will see no difference. GPU is made for floating point crunching. What most likely happens is that your ints are converted to floats before they are even uploaded to GPU. The integer functions are there for convenience, but it's all floating point on the rendering side.

Tim
  • 35,413
  • 11
  • 95
  • 121