0
votes

I am using GLSL shaders to compare textures. In one of my alghoritm's steps I want to sum up pixel values (using shaders) or calculate average of them and then read it to CPU. It may look like (shader runs on 1x1 testure, calculations only for R channel, texture2 size 1x500):

uniform sampler2D texture2;
void main()
{
    float sumR = 0.0;
    float step = 1.f / 500.f;

    for(int i = 0; i<500; i++)
    {
        vec2 pos = gl_TexCoord[0].st;
        sumR +=  (texture2D(texture2, vec2(pos.x, i * step))).r;
    }
    gl_FragColor.r =  sumR;
}

My problem is: how to store such a big number in one pixel which is 32bit (8bit for one channel only)? Or how to store and read float number with good precision in pixel?

1

1 Answers

1
votes

To store floating point values just render it to a float32 texture.

Later you can transfer the texture data to the CPU.

Why is GLSL your target and what do you try to archive, maybe an openCL implementation is a solution for your problem because you misuse the Shaders to do gerneral purpose calculations?