How to do calculation using OpenGL ES 2.0/3.0?

611 Views Asked by At

i'm thinking a problem that is it possible to do some arrays calcution using OpenGL ES in mobile devices. For example, i used glTexImage2D to pass shader a float array (which contains some 0.0 and 1.0, such as {0.0, 1.0, 1.0, 0.0, 0.0...}), and i wish to figure out how many "1" are there in this array through Shader Language. Finally return the result to CPU. Is this possible? And how can i do this?

1

There are 1 best solutions below

7
On BEST ANSWER

Well, you could try renderscript (assuming you are on android).

If it's not feasible, you could try something like this:

What you want to do could be accomplished by writing these data into a texture, where every texel is 1 or 0 depending on the data. Then you should perform something like mip-mapping on this texture until you have only 1 pixel. Then that 1 pixel will have the average value of all pixels combined, so you can calculate back exactly how many of them were 1, by multipying it with the size of your initial float array. E.g. if that 1 pixel's value is 0.25, and you had 400 data, then 100 of them had the value of 1.

Note: If the data does not fit into a texture entirely, (for example you have an odd number of data), fill the unused texels with 0.

Note2: Float precision may ruin precision if you have too much data... Make sure you are using a single channel texture format with the highest precision. Single channel 32bit textures (which you should aim for) are only available in opengl es 3.0 afaik, but check the specs.

tldr; you should make a texture, generate mipmaps for it, read the last mip level, and multiply it's value by the number of data items