Get results of GPU calculations back to the CPU program in OpenGL
Asked Answered
E

1

21

Is there a way to get results from a shader running on a GPU back to the program running on the CPU?

I want to generate a polygon mesh from simple voxel data based on a computational costly algorithm on the GPU but I need the result on the CPU for physics calculations.

Eloquence answered 29/12, 2012 at 23:40 Comment(3)
A dirty hack might be exploit CUDA<->OpenGL interop for the data copy.Leninakan
Or use OpenCL which was made for this type of task.Birmingham
related https://mcmap.net/q/116612/-how-to-debug-a-glsl-shaderShirker
M
26

Define "the results"?

In general, if you're doing GPGPU-style computations with OpenGL, you are going to need to structure your shaders around the needs of a rendering system. Rendering systems are designed to be one-way: data goes into them and an image is produced. Going backwards, having the rendering system produce data, is not generally how rendering systems are structured.

That doesn't mean you can't do it, of course. But you need to architect everything around the limitations of OpenGL.

OpenGL offers a number of hooks where you can write data from certain shader stages. Most of these require specialized hardware

Fragment shader outputs

Any hardware capable of fragment shaders will obviously allow you to write to the current framebuffer you're rendering. Through the use of framebuffer objects and textures with floating-point or integer image formats, you can write pretty much any data you want to a variety of images. Once in a texture, you can simply call glGetTexImage to get the rendered pixel data. Or you can just do glReadPixels to get it if the FBO is still bound. Either way works.

The primary limitations of this method are:

  • The number of images you can attach to the framebuffer; this limits the amount of data you can write. On pre-GL 3.x hardware, FBOs were typically limited to only 4 images plus a depth/stencil buffer. In 3.x and better hardware, you can expect a minimum of 8 images.

  • The fact that you're rendering. This means that you need to set up your vertex data to position a triangle exactly where you want it to modify data. This is not a trivial undertaking. It's also difficult to get useful input data, since you typically want each texel to be fairly independent of the other. Structuring your fragment shader around these limitations is difficult. Not impossible, but non-trivial in many cases.

Transform Feedback

This OpenGL 3.0 feature allows the output from the Vertex Processing stage of OpenGL (vertex shader and optional geometry shader) to be captured in one or more buffer objects.

This is much more natural for capturing vertex data that you want to play with or render again. In your case, you'll need to read it back after rendering it, perhaps with a glGetBufferSubData call, or by using glMapBufferRange for reading.

The limitations here are that you generally only can capture 4 output values, where each value is a vec4. There are also some strict layout restrictions. Some OpenGL 3.x and 4.x hardware offers the ability to write data to multiple feedback streams, which can all be written into different buffers.

Image Load/Store

This GL 4.2 feature represents the pinnacle of writing: you can bind an image (a buffer texture, if you want to write to a buffer), and just write to it. There are memory ordering constraints that you need to work within.

It's very flexible, but very complex. Besides the difficulty in using it properly, there are a number of limitations. The number of images you can write to will be fairly limited, perhaps 8 or so. And implementations may have total write limits, so that 8 images to write to may have to be shared by the fragment shader's outputs.

What's more, image outputs are only guaranteed for the fragment shader (and 4.3's compute shaders). That is, hardware is allowed to forbid you from using image load/store on non-FS/CS shader stages.

Minister answered 30/12, 2012 at 1:12 Comment(1)
I've got two questions regarding that: 1) Generally, after using Transform Feedback to write vertex data out from the vertex processing stage, the written data is in GPU memory and not transfered to the main memory, unless glGetBufferSubData or glMapBufferRangeare called, right? 2) If Transform Feedback and Image Load/Store operate just on GPU memory, are they equally fast or is one way faster than the other? Are they using the same kind of GPU-memory or are they operating on different types?Whizbang

© 2022 - 2024 — McMap. All rights reserved.