OpenGL: Retrieving floating point computation results from the GPU

Viewed 65

In a nutshell

In a single shader pass I'm applying a couple of different model transformation matrices in the Vertex Shader and writing the results into different position vectors. Then I do some simple arithmetic with the different results in the Vertex Shader.

//Vertex Shader
in layout(location=0) vec3 position;
in layout(location=1) vec3 vertexColor;

...

out vec3 result1;
out vec3 result2;    
out vec3 result3;
out vec3 color;

void main()
{
    gl_Position = transformationMatrix * vec4(position, 1.0);
    vec4 pos1 = transformationMatrix1 * vec4(position, 1.0);
    vec4 pos2 = transformationMatrix2 * vec4(position, 1.0);

    ... 

    result1 = pos1.xyz * pos2.xyz / 0.012313879834;
    result2 = (pos2.xyz + pos1.xyz) * 1.5;
    result3 = ....;

    color = vertexColor;
}

The results of that math I want to pass through the Fragment Shader (so the values are interpolated nicely like colors are) ...

// Fragment shader
in vec3 color;

in vec3 result1;
in vec3 result2;
in vec3 result3;

layout(location = 0) out vec4 theColor;
layout(location = 1) out vec3 output1;
layout(location = 2) out vec3 output2;
layout(location = 3) out vec3 output3;

void main()
{   
    theColor = vec4(color, 1.0);
    output1 = result1;
    output2 = result2;
}

... to finally read them back, so that I can continue working with the data on the CPU. I need the data read back to be precise (float 32) and optimally not normalized to [0, 1].

Regarding this I have a couple of questions:

  1. Initially I thought it would be possible to facilitate this using the GL_COLOR_ATTACHMENTi, but I haven't been able to figure out how. Is it possible? If so, how would I go about it?
  2. How would a solution using the Image Load/Store functionality since OpenGL 4.2 look like? Are there potential pitfalls that I need to be aware of?

EDIT: I got it to work with the Color Attachments after all. See below for a solution that worked for me.

1 Answers
Related