the_summer
Posts: 16
Joined: Mon Mar 19, 2012 9:49 am

(Un)Packing integer variables to textures

Sun Nov 16, 2014 4:05 pm

Hi all,

I want to do some multipass integer operations (basically additions and multiplications).
Thus, I need to pack my integer values somehow into the rgba-texture, so that I can use them as input in the next pass, but I have quite a lot of problems to pack and unpack the values properly on the pi.
I need to pack mostly shorts, but packing integers with up to 20 bits would be desirable.

So far I have this fragment shader functions:

Code: Select all


uniform vec2  u_texDimensions;   // image/texture dimensions
varying vec2 v_texCoord;

vec2 tex2imgCoord(in vec2 texCoord)
{
    return floor( (2.0*texCoord*u_texDimensions-1.0)/2.0+0.5 );
}


vec4 pack2shorts(in vec2 shorts)
{
    const vec4 bitSh = vec4(ONE/(f256),
                            ONE/(f256 * f256),
                            ONE/(f256),
                            ONE/(f256 * f256) );
    vec4 comp = fract(vec4(shorts.xx, shorts.yy) * bitSh);
    return floor(comp * f256) /f255;
}

vec2 unpack2shorts(in vec4 rgba)
{
   const highp vec2 bitSh = vec2(f255,
                            f256 * f255 );
    return floor(vec2(dot(rgba.xy, bitSh), dot(rgba.zw, bitSh) )+0.5);
}

void main()
{
   vec2 coord = tex2imgCoord(v_texCoord);
    vec4 temp = pack2shorts( vec2(coord.x + coord.y * u_texDimensions.x) );

    vec2 scalar = unpack2shorts ( temp );
    temp = pack2shorts(scalar);

    gl_FragColor = temp;

If I run this program with a 256x256 image it correctly creates a list from 0 to 65535 on my intel machine, but on the pi it steps over a number sometimes. I tried quite a lot of different versions to get it working properly on the pi, but with no success.
The pack2shorts-functions seems to work so far. At least if I comment the middle part of the main-function the output from my desktop and the pi are the same.

Is there someone here, who sees where I am wrong or if it is a limitation of the pi, how to work around it?

My EGL-context only creates a PBuffer with 1x1 pixels, because I only want to render into a FBO and use the result later on.
Cheers,

Jan

the_summer
Posts: 16
Joined: Mon Mar 19, 2012 9:49 am

Re: (Un)Packing integer variables to textures

Sat Nov 29, 2014 5:01 pm

To bump this thread again, I created a small test program which creates the EGL-context, runs the OpenGL shaders and checks the output. Maybe this makes it easier for others to reproduce the issue.

If I run this program on my desktop machine with Intel graphics then everything works fine (i.e. no output), but if I run it on the raspberry pi, rounding errors happen from 10988 on. I tried quite a lot of things to round between certain steps, but so far with not success.

Any help is greatly appreciated.

Cheers,

Jan
Attachments
test.tar.gz
(6.5 KiB) Downloaded 118 times

User avatar
Paeryn
Posts: 2900
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: (Un)Packing integer variables to textures

Sun Nov 30, 2014 2:19 am

You need to do the rounding correction on all 4 components when multiplying by 255 in your unpack2shorts function.

Code: Select all

vec2 unpack2shorts(in vec4 rgba)
{
   const highp vec2 bitSh = vec2(ONE, f256);
   vec4 rounded = floor((rgba * f255) + 0.5);
   return vec2(dot(rounded.xy, bitSh), dot(rounded.zw, bitSh));
}
She who travels light — forgot something.

the_summer
Posts: 16
Joined: Mon Mar 19, 2012 9:49 am

Re: (Un)Packing integer variables to textures

Sun Nov 30, 2014 4:36 pm

That's it! Thank you very much.
Now I am able to pack integers up to at least 24bit.

Return to “OpenGLES”