Gan
Gan

Reputation: 99

Get data back from OpenGL shader?

My computer doesn't support OpenCL on the GPU or OpenGL compute shaders so I was wondering if it would be a straight forward process to get data from a vertex or fragment shader?

My goal is to pass 2 textures to the shader and have the shader computer the locations where one texture exists in the other. Where there is a pixel match. I need to retrieve the locations of possible matches from the shader.

Is this plausible? If so, how would I go about it? I have the basic OpenGL knowledge, I have set up a program that draws polygons with colors. I really just need a way to get position values back from the shader.

Upvotes: 2

Views: 2856

Answers (1)

user3529622
user3529622

Reputation:

You can render to memory instead of to screen, and then fetch data from it.

  • Create and bind a Framebuffer Object
  • Create a Renderbuffer Object and attach it to the Framebuffer Object
  • Render your scene. The result will end up in the bound Framebuffer Object instead of on the screen.
  • Use glReadPixels to pull data from the Framebuffer Object.

Be aware that glReadPixels, like most methods of fetching data from GPU memory back to main memory, is slow and likely unsuitable for real-time applications. But it's the best you can do if you don't have features intended for that, like Compute Shaders, or are willing to do it asynchronously with Pixel Buffer Objects.

You can read more about Framebuffers here.

Upvotes: 2

Related Questions