GPGPU programming with OpenGL ES 2.0

为君一笑 提交于 2019-11-30 00:50:56

Basically, you need 4 vertex positions (as vec2) of a quad (with corners (-1,-1) and (1,1)) passed as a vertex attribute.

You don't really need a projection, because the shader will not need any.

Create an FBO, bind it and attach the target surface. Don't forget to check the completeness status. Bind the shader, set up input textures and draw the quad.

Your vertex shader may look like this:

#version 130
in vec2 at_pos;
out vec2 tc;
void main() {
    tc = (at_pos+vec2(1.0))*0.5;        //texture coordinates
    gl_Position = vec4(at_pos,0.0,1.0); //no projection needed
}

And a fragment shader:

#version 130
in vec2 tc;
uniform sampler2D unit_in;
void main() {
    vec4 v = texture2D(unit_in,tc);
    gl_FragColor = do_something();
}

Use this tutorial, it's targeted at OpenGL 2.0, but most features are available in ES 2.0, the only thing i have doubts is floating point textures.

http://www.mathematik.uni-dortmund.de/~goeddeke/gpgpu/tutorial.html

If you want an example, I created this project for iOS devices for processing frames of video grabbed from the camera using OpenGL ES 2.0 shaders. I explain more about it in my writeup here.

Basically, I pull in the BGRA data for a frame and create a texture from that. I then use two triangles to generate a rectangle and map the texture on that. A shader is used to directly display the image onscreen, perform some effect on the image and display it, or perform some effect on the image while in an offscreen FBO. In the last case, I can then use glReadPixels() to pull in the image for some CPU-based processing, but ideally I want to fix this so that the processed image is just passed on as a texture to the next set of shaders.

You should also check out ogles_gpgpu, which even supports Android systems. An overview about this topic is given in this publication: Parallel Computing for Digital Signal Processing on Mobile Device GPUs.

You can do more advanced GPGPU things with OpenGL ES 3.0 now. Check out this post for example. Apple now also has the "Metal API" which allows even more GPU compute operations. Both, OpenGL ES 3.x and Metal are only supported by newer devices with A7 chip.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!