问题
I am working on some code that runs some GPGPU code on Android using shaders. Usually a Framebuffer
is used so the result of the computation is stored in a texture. Also the input data is usually a texture. To improve performance it would be great to get rid of glTexImage2D
and glReadPixels
to upload and download images to and from the GPU memory space. All the code related to OpenGL is native.
On Android GraphicBuffer
can be used on Android for this propose because in mobile devices gpu memory is just host RAM memory. This structure allows to use memcpy
to transfer data with the GPU. This should be faster than using OpenGL functions.
I have been using this very cited articles [1][2][3] to successfully upload textures to the Android's GPU memory space using GraphicBuffer
.
Later I tried to use the same method to move the texture data back into CPU space but I'm still having some problems with it. According to answer [7] it should not be possible. But further reading of some other answers in StackOverflow seem to tell otherwise. Some cases of a successful replacement of glReadPixels
by a memcpy
from a GraphicBuffer
are reported in [4][5][6]. Most seem to say that placing glFinish
just before locking and reading the GraphicBuffer
makes its content updated as expected by the time it is read.
In my experience it did not work out of the box but there really seem to be a timing problem. Just calling glFinish
does not seem to be enough but if I wait a few milliseconds before reading the buffer (i.e. placing a sleep
) then the content seem to be correct. This is really not a solution as it ends up being slower than the original glReadPixels
solution (and anyway it wouldn't be a very reliable solution), but shows that the content IS actually updated at some point.
My questions would be:
Is it really true that
GraphicBuffer
can not be read directly as said in [7], or has something changed since then?Is it just a coincidence that it seem to work for other people?
Is there a better way to enforce the memory to be updated (e.g. a stronger
glFinish
call)?
来源:https://stackoverflow.com/questions/31264607/using-graphicbuffers-to-fast-read-from-a-texture